[前][次][番号順一覧][スレッド一覧]

ruby-changes:35178

From: normal <ko1@a...>
Date: Sun, 24 Aug 2014 11:03:24 +0900 (JST)
Subject: [ruby-changes:35178] normal:r47260 (trunk): lib/benchmark.rb: speedup by reducing allocations

normal	2014-08-24 11:03:06 +0900 (Sun, 24 Aug 2014)

  New Revision: 47260

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47260

  Log:
    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

  Modified files:
    trunk/ChangeLog
    trunk/lib/benchmark.rb
    trunk/test/benchmark/test_benchmark.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 47259)
+++ ChangeLog	(revision 47260)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Aug 24 10:35:54 2014  Pete Higgins  <pete@p...>
+
+	* 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@a...>
 
 	* string.c (rb_fstring): fix condition (easy to cause infinite loop!).
Index: lib/benchmark.rb
===================================================================
--- lib/benchmark.rb	(revision 47259)
+++ lib/benchmark.rb	(revision 47260)
@@ -286,13 +286,21 @@ module Benchmark https://github.com/ruby/ruby/blob/trunk/lib/benchmark.rb#L286
                        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
Index: test/benchmark/test_benchmark.rb
===================================================================
--- test/benchmark/test_benchmark.rb	(revision 47259)
+++ test/benchmark/test_benchmark.rb	(revision 47260)
@@ -152,4 +152,9 @@ BENCH https://github.com/ruby/ruby/blob/trunk/test/benchmark/test_benchmark.rb#L152
     t.add! { sleep 0.1 }
     assert_not_equal(0, t.real)
   end
+
+  def test_realtime_output
+    realtime = Benchmark.realtime { sleep 0.001 }
+    assert_in_delta 0.001, realtime
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]