Skip to content

Commit cafcc07

Browse files
authored
Merge pull request #400 from PEZ/crystal-ns-measure
In-process runner: Crystal: Use nanoseconds for the measurements
2 parents 8f18aa8 + 4a750a4 commit cafcc07

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

Diff for: lib/crystal/benchmark.cr

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ module Enumerable
1111
end
1212

1313
def bench(run_ms : Int32, &fn)
14-
times = Array(Int32).new
14+
times = Array(Int64).new
1515
result = 0
1616
secs = 0
17-
18-
while(times.sum < run_ms && !(times.sum == 0 && times.size > 0))
17+
run_ns = run_ms * 1_000_000.0
18+
19+
while(times.sum < run_ns && !(times.sum == 0 && times.size > 0))
1920
a = Time.measure {result = yield}
20-
times << a.milliseconds
21+
times << a.nanoseconds
2122

22-
if (times.sum / 1000).round > secs
23+
if (times.sum / 1_000_000_000).round > secs
2324
STDERR.print '.'
2425
secs += 1
2526
end
@@ -33,8 +34,8 @@ def format_bench(data, &formatter)
3334
raise "no data!" if data[:times].empty?
3435

3536
result = yield data[:result]
36-
times = data[:times]
37+
times = data[:times].map &./(1_000_000)
3738

3839
# mean_ms,std-dev-ms,min_ms,max_ms,times,result
39-
return "#{times.sum/times.size},#{times.std_dev},#{times.min},#{times.max},#{times.size},#{result}"
40+
"#{times.sum/times.size},#{times.std_dev},#{times.min},#{times.max},#{times.size},#{result}"
4041
end

0 commit comments

Comments
 (0)