Skip to content

Commit 6490d27

Browse files
EntityFXEntityFX
authored andcommitted
Enhance clock
1 parent f4da5e7 commit 6490d27

File tree

11 files changed

+178
-65
lines changed

11 files changed

+178
-65
lines changed

src/lua/benchmarkBase.lua

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,24 @@ BenchmarkBase = class(function(b, writer, printToConsole)
55
b.ratio = 1.0
66
b.name = "BenchmarkBase"
77
b.output = writer
8+
b.clockBefore = 0
9+
b.clockAfter = 0
10+
b.clockDiff = 0
11+
b.elapsed = 0
812
end)
913

1014
BenchmarkBase.IterrationsRatio = 1;
1115

1216
function BenchmarkBase:bench()
17+
self.clockBefore = 0
18+
self.clockAfter = 0
19+
self.clockDiff = 0
1320
self:beforeBench()
14-
local start = os.clock()
21+
self.clockBefore = clock()
1522
local res = self:benchImplementation()
23+
self.clockAfter = clock()
24+
self.elapsed = math.floor(self.clockAfter - self.clockBefore)
25+
1626
local result = self:populateResult(self:buildResult(start), res)
1727
self:doOutput(result)
1828
self:afterBench(result)
@@ -53,13 +63,12 @@ function BenchmarkBase:benchImplementation()
5363
end
5464

5565
function BenchmarkBase:buildResult(start)
56-
local elapsed = math.floor((os.clock() - start) * 1000)
5766
local tElapsed = 0
5867

59-
if elapsed == 0 then
68+
if self.elapsed == 0 then
6069
tElapsed = 1000
6170
else
62-
tElapsed = elapsed
71+
tElapsed = self.elapsed
6372
end
6473

6574
local elapsedSeconds = tElapsed / 1000

src/lua/dhrystone/dhrystone.lua

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Dhrystone2 = class(function(d, printToConsole)
1010
d.Char2Glob = "\\"
1111
d.Array1Glob = {}
1212
d.Array2Glob = {}
13+
14+
d.elapsed = 0
15+
d.startClock = 0
16+
d.endClock = 0
1317
end)
1418

1519
Dhrystone2.LOOPS = 20000000
@@ -33,6 +37,10 @@ function Dhrystone2:bench(loops)
3337
loops = Dhrystone2.LOOPS
3438
end
3539

40+
self.startClock = 0
41+
self.endClock = 0
42+
self.elapsed = 0
43+
3644
self.output:writeLine("##########################################")
3745
self.output:writeLine("")
3846
self.output:writeLine("Dhrystone Benchmark, Version 2.1 (Language: Lua)")
@@ -249,7 +257,7 @@ function Dhrystone2:Proc0(loops)
249257
local IntLoc2 = 0
250258
local IntLoc3 = 0
251259

252-
local start = os.clock()
260+
self.startClock = clock()
253261

254262
for i=0,loops - 1 do
255263
self:Proc5()
@@ -284,7 +292,10 @@ function Dhrystone2:Proc0(loops)
284292
IntLoc1 = self:Proc2(IntLoc1)
285293
end
286294

287-
local benchtime = (os.clock() - start) * 1000
295+
self.endClock = clock()
296+
self.elapsed = (self.endClock - self.startClock)
297+
298+
local benchtime = self.elapsed
288299
local loopsPerBenchtime = 0
289300
if ((benchtime == 0)) then
290301
loopsPerBenchtime = 0;

src/lua/generic/callBenchmarkBase.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function CallBenchmarBase:doCall(i, b)
1111
end
1212

1313
function CallBenchmarBase:doCallBench()
14-
local start = os.clock()
14+
local start = clock()
1515
local elapsed1 = 0
1616
local elapsed2 = 0
1717
local i = 0
@@ -23,14 +23,15 @@ function CallBenchmarBase:doCallBench()
2323
a = z + z1 + 0.5;
2424
end
2525

26-
elapsed1 = (os.clock() - start) * 1000
26+
elapsed1 = clock() - start
2727
a = 0.0;
2828
i = 0;
29-
start = os.clock()
29+
30+
start = clock()
3031
for i=1,self.iterrations do
3132
a = self:doCall(a, 0.01)
3233
end
33-
elapsed2 = (os.clock() - start) * 1000
34+
elapsed2 = clock() - start
3435

3536
self.output:write("Elapsed No Call: %d", math.floor(elapsed1))
3637
self.output:writeLine()

src/lua/generic/memoryBenchmark.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ MemoryBenchmark = class(MemoryBenchmarkBase, function(a, writer, printToConsole)
44
end)
55

66
function MemoryBenchmark:benchImplementation()
7-
return self:benchRandomMemory()
7+
return self:benchMemory()
88
end
99

1010
function MemoryBenchmark:populateResult(benchResult, memoryBenchmarkResult)

src/lua/generic/memoryBenchmarkBase.lua

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@ MemoryBenchmarkBase = class(BenchmarkBase, function(a, writer, printToConsole)
22
BenchmarkBase.init(a, writer, printToConsole)
33
a.iterrations = 500000
44
a.ratio = 1
5+
a.beforeClockMemory = 0
6+
a.afterClockMemory = 0
7+
a.elapsedClockMemory = 0
58
end)
69

7-
function MemoryBenchmarkBase:benchRandomMemory()
8-
local int4k = self:measureArrayRandomRead(1024)
10+
function MemoryBenchmarkBase:benchMemory()
11+
self.beforeClockMemory = 0
12+
self.afterClockMemory = 0
13+
self.elapsedClockMemory = 0
14+
15+
local int4k = self:measureArrayRead(1024)
916
self.output:writeLine("int 4k: %.2f MB/s", int4k.mbPerSec)
10-
local int512k = self:measureArrayRandomRead(131072)
17+
local int512k = self:measureArrayRead(131072)
1118
self.output:writeLine("int 512k: %.2f MB/s", int512k.mbPerSec)
12-
local int8m = self:measureArrayRandomRead(2097152)
19+
local int8m = self:measureArrayRead(2097152)
1320
self.output:writeLine("int 8M: %.2f MB/s", int8m.mbPerSec)
14-
local int32m = self:measureArrayRandomRead(32 * 1024 * 1024 / 4)
21+
local int32m = self:measureArrayRead(32 * 1024 * 1024 / 4)
1522
self.output:writeLine("int 32M: %.2f MB/s", int32m.mbPerSec)
1623

1724
local long4k = self:measureArrayRandomLongRead(1024)
@@ -36,7 +43,7 @@ function MemoryBenchmarkBase:benchRandomMemory()
3643
}
3744
end
3845

39-
function MemoryBenchmarkBase:measureArrayRandomRead(size)
46+
function MemoryBenchmarkBase:measureArrayRead(size)
4047
local blockSize = 16
4148
local I = {}
4249

@@ -49,6 +56,7 @@ function MemoryBenchmarkBase:measureArrayRandomRead(size)
4956
if k0 == 0 then k1 = 1 else k1 = k0 end
5057
local iterInternal = self.iterrations / k1
5158
if iterInternal == 0 then iterInternal = 1 end
59+
5260
for idx=0,endA,blockSize do
5361
I[0] = array[idx]
5462
I[1] = array[idx + 1]
@@ -67,7 +75,8 @@ function MemoryBenchmarkBase:measureArrayRandomRead(size)
6775
I[14] = array[idx + 14]
6876
I[15] = array[idx + 15]
6977
end
70-
local start = os.clock() * 1000
78+
79+
self.beforeClockMemory = clock()
7180
for i=0,iterInternal-1 do
7281
for idx=0,endA,blockSize do
7382
I[0] = array[idx]
@@ -88,7 +97,13 @@ function MemoryBenchmarkBase:measureArrayRandomRead(size)
8897
I[15] = array[idx + 15]
8998
end
9099
end
91-
local elapsed = math.floor(os.clock() * 1000 - start)
100+
self.afterClockMemory = clock()
101+
self.elapsedClockMemory = self.afterClockMemory - self.beforeClockMemory
102+
if (self.elapsedClockMemory == 0) then
103+
self.elapsedClockMemory = 1
104+
end
105+
106+
local elapsed = math.floor(self.elapsedClockMemory)
92107
return { mbPerSec = (iterInternal * #array * 8.0 / (elapsed / 1000.0) / 1024 / 1024), res = I }
93108
end
94109

@@ -115,7 +130,8 @@ function MemoryBenchmarkBase:measureArrayRandomLongRead(size)
115130
I[6] = array[idx + 6]
116131
I[7] = array[idx + 7]
117132
end
118-
local start = os.clock() * 1000
133+
134+
self.beforeClockMemory = clock()
119135
for i=0,iterInternal-1 do
120136
for idx=0,endA,blockSize do
121137
I[0] = array[idx]
@@ -128,6 +144,12 @@ function MemoryBenchmarkBase:measureArrayRandomLongRead(size)
128144
I[7] = array[idx + 7]
129145
end
130146
end
131-
local elapsed = math.floor(os.clock() * 1000 - start)
147+
self.afterClockMemory = clock()
148+
self.elapsedClockMemory = self.afterClockMemory - self.beforeClockMemory
149+
if (self.elapsedClockMemory == 0) then
150+
self.elapsedClockMemory = 1
151+
end
152+
153+
local elapsed = math.floor(self.elapsedClockMemory)
132154
return { mbPerSec = (iterInternal * #array * 8.0 / (elapsed / 1000.0) / 1024 / 1024), res = I }
133155
end

src/lua/generic/randomMemoryBenchmarkBase.lua

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ RandomMemoryBenchmarkBase = class(BenchmarkBase, function(a, writer, printToCons
22
BenchmarkBase.init(a, writer, printToConsole)
33
a.iterrations = 500000
44
a.ratio = 2
5+
a.beforeClockMemory = 0
6+
a.afterClockMemory = 0
7+
a.elapsedClockMemory = 0
58
end)
69

710
function RandomMemoryBenchmarkBase:benchRandomMemory()
11+
self.beforeClockMemory = 0
12+
self.afterClockMemory = 0
13+
self.elapsedClockMemory = 0
14+
815
local int4k = self:measureArrayRandomRead(1024)
916
self.output:writeLine("Random int 4k: %.2f MB/s", int4k.mbPerSec)
1017
local int512k = self:measureArrayRandomRead(131072)
@@ -47,13 +54,21 @@ function RandomMemoryBenchmarkBase:measureArrayRandomRead(size)
4754
for idx=0,endA do
4855
I = array[idx]
4956
end
50-
local start = os.clock() * 1000
57+
58+
self.beforeClockMemory = clock()
5159
for i=0,iterInternal-1 do
5260
for key,idx in ipairs(indexes)
5361
do
5462
I = array[idx]
5563
end
5664
end
57-
local elapsed = math.floor(os.clock() * 1000 - start)
65+
66+
self.afterClockMemory = clock()
67+
self.elapsedClockMemory = self.afterClockMemory - self.beforeClockMemory
68+
if (self.elapsedClockMemory == 0) then
69+
self.elapsedClockMemory = 1
70+
end
71+
72+
local elapsed = math.floor(self.elapsedClockMemory)
5873
return { mbPerSec = (iterInternal * #array * 8.0 / (elapsed / 1000.0) / 1024 / 1024), res = I }
5974
end

src/lua/linpack/linpack.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ end
1717

1818
function Linpack:second()
1919
if self.second_orig == -1 then
20-
self.second_orig = os.clock()
20+
self.second_orig = clock() / 1000.0
2121
end
22-
return os.clock()
22+
return clock() / 1000.0
2323
end
2424

2525
function Linpack:run_benchmark(array_size)

src/lua/main.lua

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ local writer = Writer("Output.log")
4040
BenchmarkBase.IterrationsRatio = 0.1
4141

4242
benchmarks = {
43-
ArithemticsBenchmark(writer, true),
44-
MathBenchmark(writer, true),
45-
CallBenchmark(writer, true),
46-
IfElseBenchmark(writer, true),
47-
StringManipulation(writer, true),
48-
MemoryBenchmark(writer, true),
49-
RandomMemoryBenchmark(writer, true),
50-
Scimark2Benchmark(writer, true),
43+
-- ArithemticsBenchmark(writer, true),
44+
-- MathBenchmark(writer, true),
45+
-- CallBenchmark(writer, true),
46+
-- IfElseBenchmark(writer, true),
47+
-- StringManipulation(writer, true),
48+
-- MemoryBenchmark(writer, true),
49+
-- RandomMemoryBenchmark(writer, true),
50+
-- Scimark2Benchmark(writer, true),
5151
DhrystoneBenchmark(writer, true),
52-
WhetstoneBenchmark(writer, true),
53-
LinpackBenchmark(writer, true),
54-
HashBenchmark(writer, true)
52+
-- WhetstoneBenchmark(writer, true),
53+
-- LinpackBenchmark(writer, true),
54+
-- HashBenchmark(writer, true)
5555
}
5656

5757
writer:writeHeader("Warmup")

src/lua/scimark2/scimark2.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,22 @@ function Scimark2:bench(isLarge)
9191
end
9292

9393
function Scimark2:measure(min_time, name, ...)
94+
local clockBefore = 0
95+
local clockAfter = 0
96+
local tm = 0
9497
math.randomseed( os.time() )
9598
local run = Kernel[name](...)
9699
local cycles = 1
97100
repeat
98-
local tm = os.clock()
101+
clockBefore = clock() / 1000.0
99102
local flops = run(cycles, ...)
100-
tm = os.clock() - tm
103+
clockAfter = clock() / 1000.0
104+
tm = clockAfter - clockBefore
105+
106+
if (tm == 0) then
107+
tm = 1
108+
end
109+
101110
if tm >= min_time then
102111
local res = flops / tm * 1.0e-6
103112
local p1, p2 = ...

src/lua/utils.lua

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,28 @@ end
9999
-- Returns the sum of a sequence of values
100100
function sum(x)
101101
local s = 0
102-
for _, v in ipairs(x) do s = s + v end
102+
for _, v in ipairs(x) do
103+
s = s + v
104+
end
103105
return s
104106
end
107+
108+
function clock()
109+
local clk = os.clock() * 1000
110+
return clk
111+
end
112+
113+
function clockSeconds()
114+
return clock() / 1000.0
115+
end
105116

106117
-- Calculates the arithmetic mean of a set of values
107118
-- x : an array of values
108119
-- returns : the arithmetic mean
109120
function arithmetic_mean(x)
110-
return (sum(x) / #x)
121+
local s = sum(x)
122+
local len = #x
123+
return (s / len)
111124
end
112125

113126
function compareTo(str1, str2)

0 commit comments

Comments
 (0)