Option Explicit
Public Declare Function QueryPerformanceFrequency Lib "kernel32" _
(ByRef freq As Currency) As Long
Public Declare Function QueryPerformanceCounter Lib "kernel32" _
(ByRef cnt As Currency) As Long
Private freq As Currency, df As Double
Sub doit()
Const n As Long = 10000
Dim lo As Long, hi As Long
Dim st As Single, et As Single
Dim i As Long, x As Long, s As String, fml As String
Dim dt(1 To n) As Double
Dim sc As Currency, ec As Currency, tc As Currency, dc As Currency
Dim minDC As Currency, maxDC As Currency
Dim wf As Object
Set wf = WorksheetFunction
lo = 2
hi = 1000000
Randomize
minDC = 922337203685477.5807@
maxDC = 0
tc = 0
st = Timer
For i = 1 To n
QueryPerformanceCounter sc
x = Int((hi - lo + 1) * Rnd) + lo
QueryPerformanceCounter ec
dc = ec - sc
dt(i) = convertmytimer(dc)
If dc > maxDC Then maxDC = dc
If dc < minDC Then minDC = dc
tc = tc + dc
Next
et = Timer
s = "VBA Rnd" & _
vbNewLine & "loop: " & Format(CDbl(et - st), "0.000000") & _
vbNewLine & "min: " & Format(convertmytimer(minDC), "0.000000\,000") & _
vbNewLine & "avg: " & Format(convertmytimer(tc / n), "0.000000\,000") & _
vbNewLine & "max: " & Format(convertmytimer(maxDC), "0.000000\,000") & _
vbNewLine & "min perf counter: " & Format(convertmytimer(0.0001@), "0.000000\,000000") & _
vbNewLine & "20%ile: " & Format(wf.Percentile(dt, 0.2), "0.000000\,000") & _
vbNewLine & "50%ile: " & Format(wf.Percentile(dt, 0.5), "0.000000\,000") & _
vbNewLine & "80%ile: " & Format(wf.Percentile(dt, 0.8), "0.000000\,000")
Debug.Print Now, s
MsgBox s
minDC = 922337203685477.5807@
maxDC = 0
tc = 0
st = Timer
For i = 1 To n
QueryPerformanceCounter sc
x = wf.RandBetween(lo, hi)
QueryPerformanceCounter ec
dc = ec - sc
dt(i) = convertmytimer(dc)
If dc > maxDC Then maxDC = dc
If dc < minDC Then minDC = dc
tc = tc + dc
Next
et = Timer
s = "WF.RandBetween" & _
vbNewLine & "loop: " & Format(CDbl(et - st), "0.000000") & _
vbNewLine & "min: " & Format(convertmytimer(minDC), "0.000000\,000") & _
vbNewLine & "avg: " & Format(convertmytimer(tc / n), "0.000000\,000") & _
vbNewLine & "max: " & Format(convertmytimer(maxDC), "0.000000\,000") & _
vbNewLine & "min perf counter: " & Format(convertmytimer(0.0001@), "0.000000\,000000") & _
vbNewLine & "20%ile: " & Format(wf.Percentile(dt, 0.2), "0.000000\,000") & _
vbNewLine & "50%ile: " & Format(wf.Percentile(dt, 0.5), "0.000000\,000") & _
vbNewLine & "80%ile: " & Format(wf.Percentile(dt, 0.8), "0.000000\,000")
Debug.Print Now, s
MsgBox s
minDC = 922337203685477.5807@
maxDC = 0
tc = 0
fml = "RANDBETWEEN(" & lo & "," & hi & ")"
st = Timer
For i = 1 To n
QueryPerformanceCounter sc
x = Evaluate(fml)
QueryPerformanceCounter ec
dc = ec - sc
dt(i) = convertmytimer(dc)
If dc > maxDC Then maxDC = dc
If dc < minDC Then minDC = dc
tc = tc + dc
Next
et = Timer
s = "Excel RANDBETWEEN" & _
vbNewLine & "loop: " & Format(CDbl(et - st), "0.000000") & _
vbNewLine & "min: " & Format(convertmytimer(minDC), "0.000000\,000") & _
vbNewLine & "avg: " & Format(convertmytimer(tc / n), "0.000000\,000") & _
vbNewLine & "max: " & Format(convertmytimer(maxDC), "0.000000\,000") & _
vbNewLine & "min perf counter: " & Format(convertmytimer(0.0001@), "0.000000\,000000") & _
vbNewLine & "20%ile: " & Format(wf.Percentile(dt, 0.2), "0.000000\,000") & _
vbNewLine & "50%ile: " & Format(wf.Percentile(dt, 0.5), "0.000000\,000") & _
vbNewLine & "80%ile: " & Format(wf.Percentile(dt, 0.8), "0.000000\,000")
Debug.Print Now, s
MsgBox s
minDC = 922337203685477.5807@
maxDC = 0
tc = 0
st = Timer
For i = 1 To n
QueryPerformanceCounter sc
x = Int((hi - lo + 1) * [RAND()]) + lo
QueryPerformanceCounter ec
dc = ec - sc
dt(i) = convertmytimer(dc)
If dc > maxDC Then maxDC = dc
If dc < minDC Then minDC = dc
tc = tc + dc
Next
et = Timer
s = "EXCEL RAND" & _
vbNewLine & "loop: " & Format(CDbl(et - st), "0.000000") & _
vbNewLine & "min: " & Format(convertmytimer(minDC), "0.000000\,000") & _
vbNewLine & "avg: " & Format(convertmytimer(tc / n), "0.000000\,000") & _
vbNewLine & "max: " & Format(convertmytimer(maxDC), "0.000000\,000") & _
vbNewLine & "min perf counter: " & Format(convertmytimer(0.0001@), "0.000000\,000000") & _
vbNewLine & "20%ile: " & Format(wf.Percentile(dt, 0.2), "0.000000\,000") & _
vbNewLine & "50%ile: " & Format(wf.Percentile(dt, 0.5), "0.000000\,000") & _
vbNewLine & "80%ile: " & Format(wf.Percentile(dt, 0.8), "0.000000\,000")
Debug.Print Now, s
MsgBox s
End Sub
Function convertmytimer(ByVal dt As Currency) As Double
If freq = 0 Then QueryPerformanceFrequency freq: df = freq
convertmytimer = dt / df
End Function