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

ruby-changes:37769

From: nobu <ko1@a...>
Date: Thu, 5 Mar 2015 11:39:32 +0900 (JST)
Subject: [ruby-changes:37769] nobu:r49850 (trunk): driver.rb: plain text table

nobu	2015-03-05 11:39:09 +0900 (Thu, 05 Mar 2015)

  New Revision: 49850

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

  Log:
    driver.rb: plain text table
    
    * benchmark/driver.rb (show_results): support plain text style
      table format output.

  Modified files:
    trunk/benchmark/driver.rb
Index: benchmark/driver.rb
===================================================================
--- benchmark/driver.rb	(revision 49849)
+++ benchmark/driver.rb	(revision 49850)
@@ -121,10 +121,23 @@ class BenchmarkDriver https://github.com/ruby/ruby/blob/trunk/benchmark/driver.rb#L121
   def show_results
     case @opt[:format]
     when :tsv
+      strformat = "\t%1$s"
+      numformat = "\t%1$*2$.3f"
+      minwidth = 0
+      name_width = 0
     when :markdown
       markdown = true
+      strformat = "|%1$-*2$s"
+      numformat = "|%1$*2$.3f"
+    when :plain
+      strformat = " %1$-*2$s"
+      numformat = " %1$*2$.3f"
     end
 
+    name_width ||= @name_width
+    minwidth ||= 7
+    width = @execs.map{|(_, v)| [v.size, minwidth].max}
+
     output
 
     if @verbose
@@ -151,28 +164,26 @@ class BenchmarkDriver https://github.com/ruby/ruby/blob/trunk/benchmark/driver.rb#L164
       output "minimum results in each #{@repeat} measurements."
     end
 
-    tabs = @name_width / 8
-    sep = markdown ? "\t|" : "\t"
     output "Execution time (sec)"
     output if markdown
-    output ["name", @execs.map{|(_, v)| v}].join(sep)
-    output "-"*(8*tabs+8)+"|------:"*@execs.size if markdown
-    @results.each{|v, result|
+    output ["name".ljust(name_width), @execs.map.with_index{|(_, v), i| sprintf(strformat, v, width[i])}].join("").rstrip
+    output ["-"*name_width, width.map{|n|":".rjust(n, "-")}].join("|") if markdown
+    @results.each_with_index{|(v, result), i|
       rets = []
       s = adjusted_results(v, result){|r|
-        rets << sprintf("%.3f", r)
+        rets << sprintf(numformat, r, width[i])
       }
       v += s if s
-      output [v, rets].join(sep)
+      output [v.ljust(name_width), rets].join("")
     }
 
     if @execs.size > 1
       output
       output "Speedup ratio: compare with the result of `#{@execs[0][1]}' (greater is better)"
       output if markdown
-      output ["name", @execs[1..-1].map{|(_, v)| v}].join(sep)
-      output "-"*(8*tabs+8)+"|------:"*(@execs.size-1) if markdown
-      @results.each{|v, result|
+      output ["name".ljust(name_width), @execs[1..-1].map.with_index{|(_, v), i| sprintf(strformat, v, width[i])}].join("").rstrip
+      output ["-"*name_width, width[1..-1].map{|n|":".rjust(n, "-")}].join("|") if markdown
+      @results.each_with_index{|(v, result), i|
         rets = []
         first_value = nil
         s = adjusted_results(v, result){|r|
@@ -180,14 +191,14 @@ class BenchmarkDriver https://github.com/ruby/ruby/blob/trunk/benchmark/driver.rb#L191
             if r == 0
               rets << "Error"
             else
-              rets << sprintf("%.3f", first_value/r)
+              rets << sprintf(numformat, first_value/r, width[i])
             end
           else
             first_value = r
           end
         }
         v += s if s
-        output [v, rets].join(sep)
+        output [v.ljust(name_width), rets].join("")
       }
     end
 
@@ -297,6 +308,7 @@ if __FILE__ == $0 https://github.com/ruby/ruby/blob/trunk/benchmark/driver.rb#L308
   formats = {
     :tsv => ".tsv",
     :markdown => ".md",
+    :plain => ".txt",
   }
 
   parser = OptionParser.new{|o|

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

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