I'm working on an algorithm which needs to generate millions of numbers as fast as possible. Actually I found out that the rand() function of my algorithm takes 75% of the process time.
So I'm looking for something faster. And I don't need a big range at all. (I only need integer numbers below 1000)
Do you know something I could use ?
Thanks !
Edit :
I use this numbers for shuffling groups of less than 1000 entities.
I found out more about the "fast rand". And there is SSE version version which is even faster and generates 4 numbers at a time.
rand()
is very fast. How long does your code take to run to create 1 million numbers?gprof
? If so, don't believe it, becausegprof
ignores blocked time like I/O, as well as any time taken in non-pg-compiled libraries. If you are writing these numbers somewhere, that could well be the real time-taker. I use this method.rand
is actually implemented in terms ofrandom
, which is a rather high-quality (and therefore slow) PRNG. Lower-quality ones like a pure LCG should be much faster, but might have sufficiently bad statistical properties to make them unsuitable for your application.