-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmbench_vba.bas
More file actions
110 lines (99 loc) · 2.56 KB
/
Copy pathbmbench_vba.bas
File metadata and controls
110 lines (99 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
Sub Modul1
' bmbench.c
' (c) Benchmarko, 2002
'
' 06.05.2002 0.01
' 11.05.2002 0.02 bench1 = (sum 1..n) mod 65536
'
' usage:
' bmbench [bench] [n]
'
'
' bench01
' compute (sum of 1..n) mod 65536
' in: loops = number of loops
' n = maximum number (assumed even, if integer arithmetic, normally n=1000000)
' out: x = (sum 1..n) mod 65536
'
' Loops may be increased to produce a longer runtime without
' changing the result.
'
Function bmbench01(loops, n) As Integer
Dim l As Integer
Dim i As Long
Dim x As Long
x = 0
For l = 1 To loops
For i = 1 To n
x = x + i 'overflow???
Next i
If (l < loops) Then
x = x - (n / 2) * (n + 1)
If (x <> 0) Then
MsgBox "Error: bench01: x=" & x
End If
End If
Next l
bmbench01 = x Mod 65536
End Function
'
' run a benchmark
' in: bench = benchmark to use
' loops = number of loops
' n = maximum number (used in some benchmarks to define size of workload)
' out: x = result
'
Function run_bench(bench, loops, n) As Integer
Select Case bench
Case "1"
run_bench = bmbench01(loops, n)
Case Else
MsgBox "Error: Unknown benchmark: " & bench
run_bench = -1
End Select
End Function
'
' get timestamp in milliseconds
' out: x = time in ms
'
Function get_ms() As Long
get_ms = Timer * 1000
End Function
Sub Main()
Dim start_t As Long
start_t = get_ms()
Dim bench As Integer 'benchmark to test
Dim n As Long 'maximum number
Dim min_ms As Integer 'minimum runtime for measurement in ms
bench = 1
n = 100000
min_ms = 10000
'MsgBox "DEBUG: start_t = " & start_t
MsgBox "BM Bench v0.2 (Excel/VBA)"
Dim loops As Integer 'number of loops
Dim x As Integer 'result from benchmark
Dim t1 As Long 'timestamp
loops = 1
x = 0
t1 = 0
'Calibration
While (t1 < 1000) 'we want at least 1 sec calibration time
MsgBox "Calibrating benchmark " & bench & " with loops=" & loops & ", n=" & n
t1 = get_ms()
x = run_bench(bench, loops, n)
t1 = get_ms() - t1
MsgBox "x=" & x & " (time: " & t1 & " ms)"
loops = loops * 2
Wend
loops = loops / 2
loops = loops * (min_ms / t1) + 1 'integer division!
MsgBox "Calibration done. Starting measurement with " & loops & " loops to get >=" & min_ms & " ms"
'Measurement
t1 = get_ms()
x = run_bench(bench, loops, n)
t1 = get_ms() - t1
MsgBox "x=" & x & " (time: " & t1 & " ms)"
MsgBox "Elapsed time for " & loops & " loops: " & t1 & " ms; estimation for 10 loops: " & (t1 * 10 / loops) & " ms"
MsgBox "Total elapsed time: " & (get_ms() - start_t) & " ms"
End Sub
End Sub