39 lines
778 B
Crystal
39 lines
778 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 run_n_times(ntimes : Int32, &block)
|
||
|
i = 1
|
||
|
durations = Array(Float64).new
|
||
|
while i <= ntimes
|
||
|
elapsed_time = perform_something &block
|
||
|
durations << elapsed_time.total_nanoseconds
|
||
|
i += 1
|
||
|
end
|
||
|
durations
|
||
|
end
|
||
|
|
||
|
# TODO
|
||
|
def should_nb_files(path : String, expected_nb_files : UInt32)
|
||
|
raise Exception.new "should_nb_files: not implemented yet"
|
||
|
end
|
||
|
|
||
|
def long_operation(text)
|
||
|
STDOUT.write "#{text}\r".to_slice
|
||
|
yield
|
||
|
STDOUT.write " \r".to_slice
|
||
|
end
|