With multi-threaded programs, heavy use of the rnd function (think stochastic hill-climber) will slow down the program greatly. This has been a long time problem with one of my programs and it must be some issue. Example code follows, change the thread variable from 1 to 4 and see what happens.
Is it perhaps just the overhead of the rnd function?
Code: Select all
screenres 800,600
randomize timer,4
declare sub freebasic_rnd(byval nopointer as any ptr)
declare sub custom_rnd(byval nopointer as any ptr)
dim as integer i,j,k
dim shared as integer threads=1 'change to 4 and compare timings
dim as any ptr thread_ptr(threads)
dim as double t=timer
for i=1 to threads
thread_ptr(i)=threadcreate(@freebasic_rnd,0)
sleep 10
next i
for i=1 to threads
threadwait(thread_ptr(i))
next i
print "freebasic rnd timing: "+str(timer-t)
t=timer
for i=1 to threads
thread_ptr(i)=threadcreate(@custom_rnd,0)
sleep 10
next i
for i=1 to threads
threadwait(thread_ptr(i))
next i
print "custom rnd timing: "+str(timer-t)
sleep
sub freebasic_rnd(byval nopointer as any ptr)
dim as longint i,j
for i=1 to 50000000/threads
j+=rnd*123
next i
end sub
sub custom_rnd(byval nopointer as any ptr)
dim as longint i,j,m=123
for i=1 to 50000000/threads
m=(214013*m+2531011)mod 2147483648
j+=((m shr 16)/32768)*123 '32767
next i
end sub