@@ -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
58end )
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 }
3744end
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 }
93108end
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 }
133155end
0 commit comments