summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authornormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-08-24 02:03:06 +0000
committernormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-08-24 02:03:06 +0000
commit249bd1ed2a00c9c3defdd09224b22a6691f73789 (patch)
treed27e5f87525fcb6e333766b4bdeb25ee5b31b13c /lib
parentccbeb0d6dce7e38d98232f91fc0b4bbdc499674f (diff)
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
Diffstat (limited to 'lib')
-rw-r--r--lib/benchmark.rb12
1 files changed, 10 insertions, 2 deletions
diff --git a/lib/benchmark.rb b/lib/benchmark.rb
index bd3b07284f..eff6cb12ef 100644
--- a/lib/benchmark.rb
+++ b/lib/benchmark.rb
@@ -286,13 +286,21 @@ module Benchmark
label)
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.
#
def realtime # :yield:
- r0 = Time.now
+ r0 = Process.clock_gettime(BENCHMARK_CLOCK)
yield
- Time.now - r0
+ Process.clock_gettime(BENCHMARK_CLOCK) - r0
end
module_function :benchmark, :measure, :realtime, :bm, :bmbm