why C++ – another case study?


Hello,

We did a profiling and found out the following performance bottleneck when adding many sources at once.

The code is intended to generate a unique ID everytime, but he/she is not confident that the ID will be unique every time, and that is why a While loop is used to re-generate new IDs.

random-c-sharp why C++ - another case study? c / c++ c #

random-c-sharp

Problem 1: New object is a loop (without free, well in C#, you get lazy, you don’t need manually free, and you trust Garbage collector), but this is the cause of performance bottleneck and is considered very very bad coding style.
Problem 2: It is unnecessary to generate a random number , module by 10000 and then times 10, this can be simplified to % 100000.

With C#, you can forget about freeing memory most of the time, considering this case, if you write exactly the same code in C++, you will notice the memory issues (or by Memory leak detecting tools) in the very early beginning.

Suggestions:

1. using GUID as the seed, the randomness is not good by default. check this post: https://helloacm.com/c-randomness-using-guid/

if you use GUID as seed, then collision is almost never.

2. using psudeo-random algorithms, such as this if you are geek into performance.

https://codingforspeed.com/using-faster-psudo-random-generator-xorshift/

BTW, I wrote these two posts.

Regards

—– Reply ——-

I’m quite familiar with this code – I even have added a JIRA task to rewrite it! From my recollection the ‘performance’ problem here actually comes from poor design rather than language choice. If you really want a unique ID to identify objects then GUID is precisely the type that should be used.

The ‘risk of collision’ is so low as to be considerably outweighed by other factors such corruption of RAM by cosmic rays. (See http://blogs.msdn.com/b/oldnewthing/archive/2008/06/27/8659071.aspx)

If you want an ‘id’ that can be displayed on screen in a smaller number of digits/characters then I would simply implement a specialised number-allocator class. The current approach of randomly picking a number then checking to see whether it has already been used is about the worst possible implementation that I can imagine!

That is not to deny that for certain high-performance hotspots or algorithms C++ (or indeed some other language) won’t outperform C#, but I would always look at the algorithm/design first.

—– Reply ————-

Yes, I fully agree with you. Design/Algorithms is the first thing to improve. “Premature of optimisation is the root of all evil” we need to identify the root cause of performance penalty by using profiler.

Feature Comments

A while is a conditional loop, for one… No need for an if statement. Second, this is an error with design, NOT of the language. Not knowing how to properly use your language is not a fault of the language, it is a fault of YOU, the programmer.

The brackets! The brackets should be on the same line ffs!
— If you mean the opening brackets, then you are quite incorrect. There is no “correct” way to do brackets. That being said, this style of brackets is far superior for many reasons.
Shut up seesharp boy, Java rullez !

When you run out of IDs this will hang forever… Asymptotically this isn’t great either, the time taken is a combination of random (potentially random freezes and stalls) but also becomes exponentially worse as the IDs are depleted. And the call to .Contains probably makes this n^2 to start with.

is there an additional if for the same thing as the while loop?

When you run out of IDs this will hang forever… Asymptotically this isn’t great either, the time taken is a combination of random (potentially random freezes and stalls) but also becomes exponentially worse as the IDs are depleted. And the call to .Contains probably makes this n^2 to start with

Strange that the guy was trying to assign random values in “value” keyword. He should have used _ID.

this coding style looks very familiar.

Sometimes I suppose it can be a fallback or you can use for more logic after the while, but still attached the original conditional. You can also use the if for the else statements after it. A while loop does not have an else. In this situation… it is completely pointless. lol

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
883 words
Last Post: Delphi 7 - The Classic/Best Pascal IDE for Win32 - RAD Tool
Next Post: QuickhostUK - WordPress - Brute Force Amplification Attacks Against XMLRPC

The Permanent URL is: why C++ – another case study?

4 Comments

  1. disketto
    • disketto
  2. Daniel

Leave a Reply