Optimizing random number generator (was Re: Optimizing code for tests) 
Author Message
 Optimizing random number generator (was Re: Optimizing code for tests)

Well, I have to admit.  I am surprised by the sheer amount of advise I
have gotten from my last 2 posts.  Thanks to everyone!

My original query was "how to optimize conditional branches" in a C
program.  The landslide answer was:

You cannot - the compiler can do damn near anything it wants to.  If
you want to do this sort of micro-optim work, go to assembler :-)

OK, I can live with that.  The second suggestion was to work on my
biggest time hog, the random number generator.  It runs pretty fast,
in my opinion, but takes up most of the time because of the sheer
number of times it's called.  I decided to do a profiled run last
night, and here's the prof(1) report

 %Time Seconds Cumsecs    #Calls   msec/call  Name
  41.9 7555.50 7555.50  78453355      0.0963  generate

Again, the random generator was first on the list.  This was a small
run of the simulator, nowhere near the actual size.  I do think that
prof is broken, due to the small number of calls (78 million) - I was
expecting a bit more than this.  BTW: this was run on a AT&T 3B2/400,
compiled with a good C compiler that makes use of the floating point
unit, and optimizes well.  That msec is probably milliseconds - AT&T
System V manuals don't clearly say.

So, about the generator.  It's the "ran3" generator taken from the
text _Numerical_Recipes_in_C_.  This is supposed to be derived from on
of Knuth's generators.  The whole thing works in int math, with a bit
in long integers.  It returns a float, but the division is the last
thing done.  I guess I could have it return a long and divide it
later, but I felt that having the division internal to the generator
was cleaner.

Already, I have been given the suggestion to "inline" the code, and I
will be trying that.  Are there any other suggestions that might be
helpful?  Are there better generators that this one?  Just looking for
one that is fast, has a high period, and gives an flat distribution of
numbers (not a normal distribution).

Once again, thanks in advance for any tips.

Jeffrey L. Bromberger ------- System Manager ------- Tramway Unix Systems

Thu, 02 Feb 1995 07:28:31 GMT  
 [ 1 post ] 

 Relevant Pages 

1. Optimizing code for tests

2. Optimizing programmer vs optimizing compiler (was: Re: Assembly vs c/c++)

3. Comparing 7 inlined RNG codes /Re: Timing differences for a random number generator

4. Random number generator source code

5. Help: Need C code for random number generators

6. random number generator code ?

7. random number generator random() questionable!!!?

8. Optimized code slower than debug code

9. Random number algoritms tests in C code

10. large number of draggable elements in a control (optimized etc)

11. floating point, Microsoft C optimizing, hanging Pentium, small numbers

12. Bad Address from Speed Optimized Code


Powered by phpBB® Forum Software