Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,20 @@ def realtime # :yield:
Process.clock_gettime(Process::CLOCK_MONOTONIC) - r0
end

module_function :benchmark, :measure, :realtime, :bm, :bmbm
#
# Returns the elapsed real time used to execute the given block.
# The unit of time is milliseconds.
#
# Benchmark.ms { "a" * 1_000_000_000 }
# #=> 509.8029999935534
#
def ms # :yield:
r0 = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
yield
Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - r0
end

module_function :benchmark, :measure, :realtime, :ms, :bm, :bmbm

#
# A Job is a sequence of labelled blocks to be processed by the
Expand Down
6 changes: 6 additions & 0 deletions test/benchmark/test_benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ def test_realtime_output
assert_operator sleeptime, :<, realtime
end

def test_ms_output
sleeptime = 1.0
ms_time = Benchmark.ms { sleep sleeptime }
assert_operator sleeptime * 1000, :<, ms_time
end

# Test that `to_h` returns a hash with the expected data.
def test_tms_to_h
tms = Benchmark::Tms.new(1.1, 2.2, 3.3, 4.4, 5.5, 'my label')
Expand Down
Loading