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

ruby-changes:24376

From: naruse <ko1@a...>
Date: Wed, 18 Jul 2012 12:57:11 +0900 (JST)
Subject: [ruby-changes:24376] naruse:r36427 (trunk): * lib/benchmark.rb: Fix Benchmark.benchmark output with an empty

naruse	2012-07-18 12:56:58 +0900 (Wed, 18 Jul 2012)

  New Revision: 36427

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

  Log:
    * lib/benchmark.rb: Fix Benchmark.benchmark output with an empty
      caption.  patched by Benoit Daloze. [ruby-core:45719] [Bug #6610]

  Modified files:
    trunk/ChangeLog
    trunk/lib/benchmark.rb
    trunk/test/benchmark/test_benchmark.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 36426)
+++ ChangeLog	(revision 36427)
@@ -1,3 +1,8 @@
+Wed Jul 18 12:55:54 2012  NARUSE, Yui  <naruse@r...>
+
+	* lib/benchmark.rb: Fix Benchmark.benchmark output with an empty
+	  caption.  patched by Benoit Daloze. [ruby-core:45719] [Bug #6610]
+
 Wed Jul 18 10:00:54 2012  Eric Hodel  <drbrain@s...>
 
 	* lib/debug.rb:  Added toplevel documentation.  Based on patch by Oscar
Index: lib/benchmark.rb
===================================================================
--- lib/benchmark.rb	(revision 36426)
+++ lib/benchmark.rb	(revision 36427)
@@ -169,7 +169,7 @@
     label_width ||= 0
     label_width += 1
     format ||= FORMAT
-    print ' '*label_width + caption
+    print ' '*label_width + caption unless caption.empty?
     report = Report.new(label_width, format)
     results = yield(report)
     Array === results and results.grep(Tms).each {|t|
@@ -284,7 +284,7 @@
                        t1.stime  - t0.stime,
                        t1.cutime - t0.cutime,
                        t1.cstime - t0.cstime,
-                       r1.to_f - r0.to_f,
+                       r1 - r0,
                        label)
   end
 
@@ -485,13 +485,13 @@
     #
     def format(format = nil, *args)
       str = (format || FORMAT).dup
-      str.gsub!(/(%[-+\.\d]*)n/) { "#{$1}s" % label }
-      str.gsub!(/(%[-+\.\d]*)u/) { "#{$1}f" % utime }
-      str.gsub!(/(%[-+\.\d]*)y/) { "#{$1}f" % stime }
-      str.gsub!(/(%[-+\.\d]*)U/) { "#{$1}f" % cutime }
-      str.gsub!(/(%[-+\.\d]*)Y/) { "#{$1}f" % cstime }
-      str.gsub!(/(%[-+\.\d]*)t/) { "#{$1}f" % total }
-      str.gsub!(/(%[-+\.\d]*)r/) { "(#{$1}f)" % real }
+      str.gsub!(/(%[-+.\d]*)n/) { "#{$1}s" % label }
+      str.gsub!(/(%[-+.\d]*)u/) { "#{$1}f" % utime }
+      str.gsub!(/(%[-+.\d]*)y/) { "#{$1}f" % stime }
+      str.gsub!(/(%[-+.\d]*)U/) { "#{$1}f" % cutime }
+      str.gsub!(/(%[-+.\d]*)Y/) { "#{$1}f" % cstime }
+      str.gsub!(/(%[-+.\d]*)t/) { "#{$1}f" % total }
+      str.gsub!(/(%[-+.\d]*)r/) { "(#{$1}f)" % real }
       format ? str % args : str
     end
 
@@ -554,7 +554,7 @@
 
   n = ARGV[0].to_i.nonzero? || 50000
   puts %Q([#{n} times iterations of `a = "1"'])
-  benchmark("       " + CAPTION, 7, FORMAT) do |x|
+  benchmark(CAPTION, 7, FORMAT) do |x|
     x.report("for:")   {for _ in 1..n; _ = "1"; end} # Benchmark.measure
     x.report("times:") {n.times do   ; _ = "1"; end}
     x.report("upto:")  {1.upto(n) do ; _ = "1"; end}
Index: test/benchmark/test_benchmark.rb
===================================================================
--- test/benchmark/test_benchmark.rb	(revision 36426)
+++ test/benchmark/test_benchmark.rb	(revision 36427)
@@ -4,7 +4,7 @@
 MiniTest::Unit.autorun
 
 describe Benchmark do
-  BENCH_FOR_TIMES_UPTO = lambda do |x|
+  bench_for_times_upto = lambda do |x|
     n = 1000
     tf = x.report("for:")   { for _ in 1..n; '1'; end }
     tt = x.report("times:") { n.times do   ; '1'; end }
@@ -12,21 +12,23 @@
     [tf+tt+tu, (tf+tt+tu)/3]
   end
 
-  BENCH_FOR_TIMES_UPTO_NO_LABEL = lambda do |x|
+  bench_for_times_upto_no_label = lambda do |x|
     n = 1000
     x.report { for _ in 1..n; '1'; end }
     x.report { n.times do   ; '1'; end }
     x.report { 1.upto(n) do ; '1'; end }
   end
 
-  LABELS = %w[first second third]
+  def labels
+    %w[first second third]
+  end
 
   def bench(type = :bm, *args, &block)
     if block
       Benchmark.send(type, *args, &block)
     else
       Benchmark.send(type, *args) do |x|
-        LABELS.each { |label|
+        labels.each { |label|
           x.report(label) {}
         }
       end
@@ -59,12 +61,29 @@
     end
   end
 
+  benchmark_output_with_total_avg = <<BENCH
+              user     system      total        real
+for:      --time--   --time--   --time-- (  --time--)
+times:    --time--   --time--   --time-- (  --time--)
+upto:     --time--   --time--   --time-- (  --time--)
+>total:   --time--   --time--   --time-- (  --time--)
+>avg:     --time--   --time--   --time-- (  --time--)
+BENCH
+
   describe 'benchmark' do
+    it 'does not print any space if the given caption is empty' do
+      capture_bench_output(:benchmark).must_equal <<-BENCH
+first  --time--   --time--   --time-- (  --time--)
+second  --time--   --time--   --time-- (  --time--)
+third  --time--   --time--   --time-- (  --time--)
+BENCH
+    end
+
     it 'makes extra calcultations with an Array at the end of the benchmark and show the result' do
       capture_bench_output(:benchmark,
         Benchmark::CAPTION, 7,
         Benchmark::FORMAT, ">total:", ">avg:",
-        &BENCH_FOR_TIMES_UPTO).must_equal BENCHMARK_OUTPUT_WITH_TOTAL_AVG
+        &bench_for_times_upto).must_equal benchmark_output_with_total_avg
     end
   end
 
@@ -74,8 +93,8 @@
         capture_io do
           results = bench(meth)
           results.must_be_instance_of Array
-          results.size.must_equal LABELS.size
-          results.zip(LABELS).each { |tms, label|
+          results.size.must_equal labels.size
+          results.zip(labels).each { |tms, label|
             tms.must_be_instance_of Benchmark::Tms
             tms.label.must_equal label
           }
@@ -84,26 +103,49 @@
     end
 
     it 'correctly output when the label width is given' do
-      capture_bench_output(:bm, 6).must_equal BM_OUTPUT
+      capture_bench_output(:bm, 6).must_equal <<-BENCH
+             user     system      total        real
+first    --time--   --time--   --time-- (  --time--)
+second   --time--   --time--   --time-- (  --time--)
+third    --time--   --time--   --time-- (  --time--)
+BENCH
     end
 
     it 'correctly output when no label is given' do
-      capture_bench_output(:bm, &BENCH_FOR_TIMES_UPTO_NO_LABEL).must_equal BM_OUTPUT_NO_LABEL
+      capture_bench_output(:bm, &bench_for_times_upto_no_label).must_equal <<-BENCH
+       user     system      total        real
+   --time--   --time--   --time-- (  --time--)
+   --time--   --time--   --time-- (  --time--)
+   --time--   --time--   --time-- (  --time--)
+BENCH
     end
 
     it 'can make extra calcultations with an array at the end of the benchmark' do
       capture_bench_output(:bm, 7, ">total:", ">avg:",
-        &BENCH_FOR_TIMES_UPTO).must_equal BENCHMARK_OUTPUT_WITH_TOTAL_AVG
+        &bench_for_times_upto).must_equal benchmark_output_with_total_avg
     end
   end
 
   describe 'bmbm' do
+    bmbm_output = <<BENCH
+Rehearsal ------------------------------------------
+first    --time--   --time--   --time-- (  --time--)
+second   --time--   --time--   --time-- (  --time--)
+third    --time--   --time--   --time-- (  --time--)
+--------------------------------- total: --time--sec
+
+             user     system      total        real
+first    --time--   --time--   --time-- (  --time--)
+second   --time--   --time--   --time-- (  --time--)
+third    --time--   --time--   --time-- (  --time--)
+BENCH
+
     it 'correctly guess the label width even when not given' do
-      capture_bench_output(:bmbm).must_equal BMBM_OUTPUT
+      capture_bench_output(:bmbm).must_equal bmbm_output
     end
 
     it 'correctly output when the label width is given (bmbm ignore it, but it is a frequent mistake)' do
-      capture_bench_output(:bmbm, 6).must_equal BMBM_OUTPUT
+      capture_bench_output(:bmbm, 6).must_equal bmbm_output
     end
   end
 
@@ -125,39 +167,3 @@
     end
   end
 end
-
-BM_OUTPUT = <<BENCH
-             user     system      total        real
-first    --time--   --time--   --time-- (  --time--)
-second   --time--   --time--   --time-- (  --time--)
-third    --time--   --time--   --time-- (  --time--)
-BENCH
-
-BM_OUTPUT_NO_LABEL = <<BENCH
-       user     system      total        real
-   --time--   --time--   --time-- (  --time--)
-   --time--   --time--   --time-- (  --time--)
-   --time--   --time--   --time-- (  --time--)
-BENCH
-
-BMBM_OUTPUT = <<BENCH
-Rehearsal ------------------------------------------
-first    --time--   --time--   --time-- (  --time--)
-second   --time--   --time--   --time-- (  --time--)
-third    --time--   --time--   --time-- (  --time--)
---------------------------------- total: --time--sec
-
-             user     system      total        real
-first    --time--   --time--   --time-- (  --time--)
-second   --time--   --time--   --time-- (  --time--)
-third    --time--   --time--   --time-- (  --time--)
-BENCH
-
-BENCHMARK_OUTPUT_WITH_TOTAL_AVG = <<BENCH
-              user     system      total        real
-for:      --time--   --time--   --time-- (  --time--)
-times:    --time--   --time--   --time-- (  --time--)
-upto:     --time--   --time--   --time-- (  --time--)
->total:   --time--   --time--   --time-- (  --time--)
->avg:     --time--   --time--   --time-- (  --time--)
-BENCH

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

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