33 lines
658 B
Crystal
33 lines
658 B
Crystal
|
def perform_something(&block)
|
|||
|
start = Time.monotonic
|
|||
|
yield
|
|||
|
Time.monotonic - start
|
|||
|
end
|
|||
|
|
|||
|
def perform_benchmark_average(ntimes : Int32, &block)
|
|||
|
i = 1
|
|||
|
sum = Time::Span.zero
|
|||
|
while i <= ntimes
|
|||
|
elapsed_time = perform_something &block
|
|||
|
sum += elapsed_time
|
|||
|
i += 1
|
|||
|
end
|
|||
|
|
|||
|
sum / ntimes
|
|||
|
end
|
|||
|
|
|||
|
def perform_benchmark_average_verbose(title : String, ntimes : Int32, &block)
|
|||
|
i = 1
|
|||
|
sum = Time::Span.zero
|
|||
|
puts "Execute '#{title}' × #{ntimes}"
|
|||
|
while i <= ntimes
|
|||
|
elapsed_time = perform_something &block
|
|||
|
sum += elapsed_time
|
|||
|
STDOUT.write "\relapsed_time: #{elapsed_time}, average: #{sum/i}".to_slice
|
|||
|
|
|||
|
i += 1
|
|||
|
end
|
|||
|
puts ""
|
|||
|
puts "Average: #{sum/ntimes}"
|
|||
|
end
|