r/dailyprogrammer • • May 16 '12

[5/16/2012] Challenge #53 [intermediate]

A simple pseudo-random number generator looks like this:

s(0) = 123456789
s(n) = (22695477 * s(n-1) + 12345) mod 1073741824

So each number is generated from the previous one.

Using this generator, generate 10 million numbers (i.e. s(0) through s(9,999,999)) and find the 1000 largest numbers in that list. What is the sum of those numbers?

Try to make your solution as efficient as possible.

  • Thanks to sim642 for submitting this problem in /r/dailyprogrammer_ideas! If you have a problem that you think would be good, head on over there and help us out!
11 Upvotes

19 comments sorted by

View all comments

Show parent comment

1

u/leonardo_m May 22 '12

Which compilers and optimization levels did you use?

GCC 4.7.0, -Ofast -flto -s

Last I heard, D compilers lagged behind C compilers with regards to optimizations

If in D you use only C constructs/features, you get a program about as efficient as the equivalent C program compiled with the same back-end. So in that case if you use an efficient back-end (GDC2 or LDC2) your D code will be fast. If in D you use higher level constructs, the efficiency will vary (example: the GC is not very efficient. Array literal assignments cause useless heap activity. If you use the not efficient DMD back-end integer divisions with small divisors are not optimized. And so on). But my last comment there was only about the C versions of the code (the short D version I've shown is designed for brevity. Using the not efficient DMD compiler it's more than twice slower than your C version).

I don't get why memmove would be faster,

Modern CPUs are very complex beasts, and Agner Fog shows that one of the operations they most love are linear scans on small arrays that have a compile-time-known length. I have timed many variants of your simple C code, and they were all slower. Linked lists, especially ones that require mixed direction jumps, are not so performance-efficient, scanning the linked list was slow. Even a binary search wasn't faster than a linear search + memmove on that 8000 bytes long max_nums array, even a binary heap, that avoids the logarithmic search and replaces the memmove with a logaritmic number of swaps was slower, I am not sure why. Even using a linear scan adding a ULLONG_MAX sentinel past the end of max_nums, using a [MAX_COUNT + 1] array, to remove the "k<MAX_COUNT &&" has not improved the run-time, again I don't know why, similar sentinels used to improve performance on critical loops. But maybe today for the CPU it's more important to know the length of the small array at compile time.

You've made me thoroughly curious about this....

Maybe I have done some experimental mistakes, so if you find a faster version I'd like to see it.