Here is an ordinary mean value. I don't think that the distribution of timings is uniform, the deviations are much less than the expected deviations.
Also I have calculated a loop dead time close to each loop.
Same thing, array index versus pointer index on the stack.
Code: Select all
function deadtime(num as ulong=10000000) as double
dim as double t=timer
for n as long=1 to num
next
return timer-t
end function
function mean(a() as double,byref sd as double=0,byref min as double=0,byref max as double=0) as double
min=1e6:max=-1e6
dim as long lb=lbound(a),ub=ubound(a)
dim as double acc,m
for n as long=lb to ub
acc+=a(n)
if max<a(n) then max=a(n)
if min>a(n) then min=a(n)
next
m=acc/(ub-lb+1)
acc=0
for n as long=lb to ub
acc+=(a(n)-m)*(a(n)-m)
next
sd =sqr(acc/(ub-lb))
return m
end function
#macro external(pd)
#endmacro
'==================================================
'On the stack
Dim As Integer a(10000)
Dim As Integer Ptr ap=@a(0)
a(2017)=131:a(96)=30
Dim As Double t
Dim As longint i
dim as double PDA,PDP ' "" for Paul doe function
dim as long trials=20
dim as long limit=20000000
dim as double ma(1 to trials),mp(1 to trials)
sleep 100
For z As Long=1 To trials
if rnd >.5 then 'which one first?
i=0
t=timer
For n As Long=1 To limit
i+=a(2017)-a(96)
Next
t=timer-t-deadtime
ma(z)=t 'store each time
external(PDA)
Print t,i,"array element"
Sleep 50
i=0
t=Timer
For n As Long=1 To limit
i+=ap[2017] -ap[96]
Next
t=timer-t-deadtime
mp(z)=t 'store each time
external(PDP)
Print t ,i,"pointer element"
Print
sleep 50
else
i=0
t=Timer
For n As Long=1 To limit
i+=ap[2017] -ap[96]
Next
t=timer-t-deadtime
mp(z)=t 'store each time
external(PDP)
Print t ,i,"pointer element"
sleep 50
i=0
t=timer
For n As Long=1 To limit
i+=a(2017)-a(96)
Next
t=timer-t-deadtime
ma(z)=t 'store each time
external(PDA)
Print t,i,"array element"
sleep 50
Print
end if
Next z
dim as double sd,max,min
'Delete[] ap
print
print
print tab(45);"Deviations from mean expected for uniform"
print "Mean array time "; mean(ma(),sd,min,max),sd,sqr((max-min)/12)
print "Mean pointer time "; mean(mp(),sd,min,max),sd,sqr((max-min)/12)
print "deadtime approx ";deadtime
Sleep