Actions
Feature #10165
closedUse Process.clock_gettime to speed up Benchmark.realtime.
Feature #10165: Use Process.clock_gettime to speed up Benchmark.realtime.
Status:
Closed
Assignee:
-
Target version:
-
Description
This patch changes the Benchmark.realtime method to use the Process.clock_gettime internally when generating the time elapsed. Calling Process.clock_gettime is faster than the current way of creating Time objects.
I wrote a benchmark script (also attached) to demonstrate the difference:
require 'benchmark' def old_benchmark r0 = Time.now yield Time.now - r0 end def new_benchmark r0 = Process.clock_gettime(Process::CLOCK_MONOTONIC) yield Process.clock_gettime(Process::CLOCK_MONOTONIC) - r0 end n = (ARGV.first || 1_000_000).to_i puts "#{n} iterations." Benchmark.bmbm do |b| b.report("old") { n.times { old_benchmark { nil } } } b.report("new") { n.times { new_benchmark { nil } } } end When I run this on my local machine I see this output:
1000000 iterations. Rehearsal --------------------------------------- old 0.860000 0.000000 0.860000 ( 0.863118) new 0.360000 0.000000 0.360000 ( 0.355242) ------------------------------ total: 1.220000sec user system total real old 0.870000 0.010000 0.880000 ( 0.866577) new 0.330000 0.000000 0.330000 ( 0.328982) I discussed this idea originally with Eric Hodel, but he has not reviewed this code.
Files
Actions