# Use of a table of pre-calculated RND results to speed up program

April 24 2004 at 1:33 PM

Response to More discussion on RND

ToohTooh suggested that by pre-calculating RND*X values, rather than computing them dynamically, a significant improvement will result.

First, I ran the program shown below which includes a version of ToohTooh's function and got an improvement of 5.15 seconds as opposed to 16.8.

On the surface, that looks great, but then I tried a "practical application"
http://www.network54.com/Forum/message?forumid=190883&messageid=1082838175
which showed 20 images in 38 seconds as opposed to 40 seconds.

Two seconds might not be worth the effort, but a good technique to keep in mind.

Mac

DECLARE FUNCTION randomNrGen% ()
DIM maxRandom AS INTEGER
maxRandom = 10000
DIM SHARED randomTable(maxRandom) AS INTEGER

'Get a randomizer seed (x0 in the formula)
seed& = TIMER
RANDOMIZE seed&

'Let it begin -- make it a big prime number that is meaningful to QBasic TIMER
' on today's computers.

'Run 5 tests and get the average.

'Test every piece alone and don't put them within another loop because if you
' compiled this at QB 4.0 and higher within a different program, BC may place
' back-loop-pointer (NEXT keyword's pointer) in far memory which will result
' in switching segments (and thus, corrupting our results).

'Why piece by piece? We should see them perform within a block as BC may
' perform some optimizations.

'First off, our traditional RND. We need to explicitly define an upper bound.
startIt! = TIMER
FOR j = 1 TO 10
FOR i& = 0 TO 130002: a% = RND * 999: NEXT i&
FOR i& = 0 TO 130002: a% = RND * 999: NEXT i&
FOR i& = 0 TO 130002: a% = RND * 999: NEXT i&
FOR i& = 0 TO 130002: a% = RND * 999: NEXT i&
FOR i& = 0 TO 130002: a% = RND * 999: NEXT i&
NEXT j
stopIt! = TIMER
PRINT "Traditional RND performance was:"; (stopIt! - startIt!) / 5

'Now the pseudo-random generator.
startIt! = TIMER
' Prepare the table at run-time.
FOR i = 0 TO maxRandom
'Define the upper limit (which is 999) here.
randomTable(i) = RND * 999
NEXT i
FOR j = 1 TO 10
FOR i& = 0 TO 130002: a% = randomNrGen%: NEXT i&
FOR i& = 0 TO 130002: a% = randomNrGen%: NEXT i&
FOR i& = 0 TO 130002: a% = randomNrGen%: NEXT i&
FOR i& = 0 TO 130002: a% = randomNrGen%: NEXT i&
FOR i& = 0 TO 130002: a% = randomNrGen%: NEXT i&
NEXT j
stopIt! = TIMER
PRINT "Pseudo-random performance was:"; (stopIt! - startIt!) / 5

'If you don't live on Mars, pseudo-random's performance will be greater. Even
' if we do *not* define an upper limit in RND test (which is very unlikely
' in real world), the RND would again be slower (give it a try).

'Note, however, that, although this is random enough for many applications,
' for statistical and artificial intelligence simulations, a better randomiz-
' er should be used.

'The point is that, we, again, see the trade-offs of gaining-in-time.

'All the best,
' ToohTooh

FUNCTION randomNrGen%
DIM maxRandom AS INTEGER
STATIC nextRandom AS INTEGER
maxRandom = UBOUND(randomTable)
randomNrGen% = randomTable(nextRandom)
nextRandom = (nextRandom + 1) MOD (maxRandom + 1)
END FUNCTION

 Respond to this message
Responses