I don't understand your argument, the way I see it the random calls negate each other. a real benchmark should take into consideration real tests not just "how fast is this code executing by itself" because the latter is no good in reality, or is it?
Code: Select all
;---
Macro decimals( _n_ ) : (_n_-Int(_n_)) : EndMacro
Macro decimals2( _n_, _result_ )
!push $1f7f0000 ; FP control word needed to round to zero
!fstcw [esp] ; Save the current FP control word
!fldcw [esp+2] ; Set the FP to round to zero
!fld dword[v_#_n_] ; Load the FP with the number to work on
!fld st0 ; Replicate/push that number on the FP stack
!frndint ; Round number to leave integer
!fsubp st1,st0 ; Subtract from original to leave fraction part (and pop stack)
!fldcw [esp] ; Restore original FP control word
!fstp dword[v_#_result_]
!add esp,4 ; Clean up CPU stack
EndMacro
;---
Macro TestNumber
Random(10)+Random(10)*0.1 ; 123.456 ;
EndMacro
Macro TestCaseA
Define.f a_temp = TestNumber
Define.f a_temp_decimals
decimals2(a_temp, a_temp_decimals)
EndMacro
Macro TestCaseB
Define.f b_temp = TestNumber
Define.f b_temp_decimals = decimals(b_temp)
EndMacro
;---
#BENCHMARK_ITERATIONS = 100000000;0;0
SetPriorityClass_( GetCurrentProcess_(), #REALTIME_PRIORITY_CLASS)
Macro InitTestCase
Delay(1000)
RandomSeed(123456789)
EndMacro
;---
Define.i a_time_old, a_time_new
Define.i b_time_old, b_time_new
InitTestCase
a_time_old = ElapsedMilliseconds()
For i=1 To #BENCHMARK_ITERATIONS
TestCaseA
Next
a_time_new = ElapsedMilliseconds()
InitTestCase
b_time_old = ElapsedMilliseconds()
For i=1 To #BENCHMARK_ITERATIONS
TestCaseB
Next
b_time_new = ElapsedMilliseconds()
;---
Define.i a_result, b_result
Define.f a_percent, b_percent
a_result = ( a_time_new - a_time_old )
b_result = ( b_time_new - b_time_old )
a_percent = (( a_result / b_result ) * 100)
b_percent = (( b_result / a_result ) * 100)
Define.s result
result + #CRLF$
result + "A= " + Str(a_result) + "ms" + #LF$
result + "B= " + Str(b_result) + "ms" + #LF$
result + #CRLF$
result + StrF( a_percent, 2 ) +"%"+ " ( "+StrF( 100-a_percent, 2 )+"% )" + #LF$
result + StrF( b_percent, 2 ) +"%"+ " ( "+StrF( 100-b_percent, 2 )+"% )" + #LF$
result + #CRLF$
MessageRequester( "benchmark result", result )
no need for a higher resolution timer (tried, same results... it's a big scale). there should be a min or max routine to make more sense of the results but I left that out.
if you benchmark the random calls on each test, you'll see it takes the same time on both cases (with a margin of 0.1 due to the low resolution of the timer) hence they are "negating" each other in the test I just pasted.
so, there is no need to initialize the random numbers in an array at all but it would make for a better test.