From 8fded5ba3e464277eb5bce0717a168ae53f3d0f6 Mon Sep 17 00:00:00 2001 From: hsbt Date: Wed, 23 Jul 2014 19:45:09 +0000 Subject: * lib/benchmark.rb: split executable code into sample directory. * sample/benchmark.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46922 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- sample/benchmark.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 sample/benchmark.rb (limited to 'sample/benchmark.rb') diff --git a/sample/benchmark.rb b/sample/benchmark.rb new file mode 100644 index 0000000000..de5d66f505 --- /dev/null +++ b/sample/benchmark.rb @@ -0,0 +1,19 @@ +require 'benchmark' + +include Benchmark + +n = ARGV[0].to_i.nonzero? || 50000 +puts %Q([#{n} times iterations of `a = "1"']) +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} +end + +benchmark do + [ + measure{for _ in 1..n; _ = "1"; end}, # Benchmark.measure + measure{n.times do ; _ = "1"; end}, + measure{1.upto(n) do ; _ = "1"; end} + ] +end -- cgit v1.2.3