lib/benchmark.rb: speedup by reducing allocations
* lib/benchmark.rb (module Benchmark): define BENCHMARK_CLOCK (realtime): use Process.clock_gettime(BENCHMARK_CLOCK) [Feature #10165] * test/benchmark/test_benchmark.rb (test_realtime_output): new test git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47260 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
ccbeb0d6dc
commit
249bd1ed2a
@ -1,3 +1,11 @@
|
|||||||
|
Sun Aug 24 10:35:54 2014 Pete Higgins <pete@peterhiggins.org>
|
||||||
|
|
||||||
|
* lib/benchmark.rb (module Benchmark): define BENCHMARK_CLOCK
|
||||||
|
(realtime): use Process.clock_gettime(BENCHMARK_CLOCK)
|
||||||
|
Reduces allocations to improve performance [Feature #10165]
|
||||||
|
|
||||||
|
* test/benchmark/test_benchmark.rb (test_realtime_output): new test
|
||||||
|
|
||||||
Fri Aug 22 20:23:54 2014 Koichi Sasada <ko1@atdot.net>
|
Fri Aug 22 20:23:54 2014 Koichi Sasada <ko1@atdot.net>
|
||||||
|
|
||||||
* string.c (rb_fstring): fix condition (easy to cause infinite loop!).
|
* string.c (rb_fstring): fix condition (easy to cause infinite loop!).
|
||||||
|
@ -286,13 +286,21 @@ module Benchmark
|
|||||||
label)
|
label)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# :stopdoc:
|
||||||
|
if defined?(Process::CLOCK_MONOTONIC)
|
||||||
|
BENCHMARK_CLOCK = Process::CLOCK_MONOTONIC
|
||||||
|
else
|
||||||
|
BENCHMARK_CLOCK = Process::CLOCK_REALTIME
|
||||||
|
end
|
||||||
|
# :startdoc:
|
||||||
|
|
||||||
#
|
#
|
||||||
# Returns the elapsed real time used to execute the given block.
|
# Returns the elapsed real time used to execute the given block.
|
||||||
#
|
#
|
||||||
def realtime # :yield:
|
def realtime # :yield:
|
||||||
r0 = Time.now
|
r0 = Process.clock_gettime(BENCHMARK_CLOCK)
|
||||||
yield
|
yield
|
||||||
Time.now - r0
|
Process.clock_gettime(BENCHMARK_CLOCK) - r0
|
||||||
end
|
end
|
||||||
|
|
||||||
module_function :benchmark, :measure, :realtime, :bm, :bmbm
|
module_function :benchmark, :measure, :realtime, :bm, :bmbm
|
||||||
|
@ -152,4 +152,9 @@ BENCH
|
|||||||
t.add! { sleep 0.1 }
|
t.add! { sleep 0.1 }
|
||||||
assert_not_equal(0, t.real)
|
assert_not_equal(0, t.real)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_realtime_output
|
||||||
|
realtime = Benchmark.realtime { sleep 0.001 }
|
||||||
|
assert_in_delta 0.001, realtime
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user