Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added an option to not run a benchmark on an implementation

  • Loading branch information...
commit 123ae3d4089b3746bb98659bf8b74a118db29ab5 1 parent 02f5a49
@mattetti authored
Showing with 101 additions and 97 deletions.
  1. +58 −49 io.rb
  2. +43 −48 struct.rb
View
107 io.rb
@@ -1,33 +1,38 @@
-require 'benchmark'
+require File.expand_path('herbs_helper')
n = 4_000
o = 10_000
p = 50_000
+
+def clear_ios
+ ObjectSpace.each_object(IO) {|x| x.close unless (x.closed? || x.inspect =~ /STD|fixtures/i) }
+end
+
Benchmark.bm do |b|
small_origin = File.open('fixtures/jules.txt')
large_origin = File.open('fixtures/verne.txt')
- b.report("new\n") do
+ b.bench("new\n", :macruby => false) do
p.times do
IO.new( 1, "w" )
end
- end
+ end
if IO.respond_to?(:binread)
- b.report("binread\n") do
+ b.bench("binread\n") do
n.times do
IO.binread('fixtures/verne.txt')
end
end
end
- b.report("copy_stream (small file)\n") do
+ b.bench("copy_stream (small file)\n", :macruby => false) do
n.times do
IO.copy_stream('fixtures/jules.txt', 'fixtures/tmp', 10, 10)
end
end
- b.report("copy_stream (big file)\n") do
+ b.bench("copy_stream (big file)\n", :macruby => false) do
n.times do
IO.copy_stream('fixtures/verne.txt', 'fixtures/tmp', 40, 40)
end
@@ -35,132 +40,136 @@
destination = File.open('fixtures/tmp', 'w+')
# not running on MacRuby atm
- # b.report("copy_stream using IO sources (small file)\n") do
- # n.times do
- # IO.copy_stream(small_origin, destination, 10, 10)
- # end
- # end
+ b.bench("copy_stream using IO sources (small file)\n", :macruby => false) do
+ n.times do
+ IO.copy_stream(small_origin, destination, 10, 10)
+ end
+ end
- # b.report("copy_stream using IO sources (big file)\n") do
- # n.times do
- # IO.copy_stream(large_origin, destination, 40, 40)
- # end
- # end
+ b.bench("copy_stream using IO sources (big file)\n", :macruby => false) do
+ n.times do
+ IO.copy_stream(large_origin, destination, 40, 40)
+ end
+ end
- b.report("foreach (small file)\n") do
+ b.bench("foreach (small file)\n") do
o.times do
IO.foreach('fixtures/jules.txt'){|x| x }
end
end
- b.report("foreach (large file)\n") do
+ b.bench("foreach (large file)\n") do
100.times do
IO.foreach('fixtures/verne.txt'){|x| x }
end
end
- b.report("open\n") do
+ b.bench("open\n", :macruby => false) do
p.times do |i|
IO.open(1, "w"){|io| }
end
end
- b.report("pipe\n") do
+ b.bench("pipe\n") do
25_000.times do |i|
rd, wr = IO.pipe
rd.close
wr.close
end
end
+
+ clear_ios
- b.report("popen\n") do
+ b.bench("popen\n") do
150.times do |i|
IO.popen("uname")
end
- end
+ end
- ObjectSpace.each_object(IO) {|x| x.close unless (x.closed? || x.inspect =~ /STD|fixtures/i) }
+ clear_ios
- b.report("read (small file)\n") do
+ b.bench("read (small file)\n") do
n.times do
IO.read('fixtures/jules.txt')
end
end
- b.report("read (big file)\n") do
+ b.bench("read (big file)\n") do
o.times do
IO.read('fixtures/verne.txt', 1_000)
end
end
- b.report("readlines (small file)\n") do
+ b.bench("readlines (small file)\n") do
o.times do
IO.readlines('fixtures/jules.txt')
end
end
- b.report("readlines (large file)\n") do
+ b.bench("readlines (large file)\n") do
150.times do
IO.readlines('fixtures/verne.txt')
end
end
-
- b.report("sysopen (small file)\n") do
- n.times do
- IO.sysopen('fixtures/jules.txt')
- end
- end
- ObjectSpace.each_object(IO) {|x| x.close unless (x.closed? || x.inspect =~ /STD|fixtures/i) }
-
- b.report("sysopen (large file)\n") do
- 3_000.times do
- IO.sysopen('fixtures/verne.txt')
- end
- end
+ clear_ios
- ObjectSpace.each_object(IO) {|x| x.close unless (x.closed? || x.inspect =~ /STD|fixtures/i) }
+ # b.bench("sysopen (small file) & close\n") do
+ # 250.times do
+ # IO.sysopen('fixtures/jules.txt')
+ # end
+ # end
+ #
+ # ObjectSpace.each_object(IO) {|x| x.close unless (x.closed? || x.inspect =~ /STD/i) }
+ #
+ # b.bench("sysopen (large file)\n") do
+ # 150.times do |n|
+ # IO.sysopen('fixtures/verne.txt')
+ # end
+ # end
+ #
+ # clear_ios
class DummyIO; def to_io; STDOUT; end; end
dio = DummyIO.new
- b.report("try_convert obj\n") do
+ b.bench("try_convert obj\n") do
p.times do
IO.try_convert(dio)
end
end
- b.report("try_convert str\n") do
+ b.bench("try_convert str\n") do
p.times do
IO.try_convert("dio")
end
end
- b.report("<<\n") do
+ b.bench("<<\n") do
100_000.times do
STDOUT << ""
end
end
- b.report("binmode\n") do
+ b.bench("binmode\n") do
10_000.times do
i = IO.new(1); i.binmode;
end
end
- b.report("binmode?\n") do
+ b.bench("binmode?\n", :macruby => false) do
200_000.times do
small_origin.binmode?
end
- end
+ end
- b.report("chars (small file)\n") do
+ b.bench("chars (small file)\n") do
p.times do
small_origin.chars
end
end
- b.report("chars (large file)\n") do
+ b.bench("chars (large file)\n") do
p.times do
large_origin.chars
end
View
91 struct.rb
@@ -1,90 +1,85 @@
-require 'benchmark'
+require File.expand_path('herbs_helper')
Contact = Struct.new(:first_name, :last_name, :home_number, :cell_number, :email )
-n = 10_000
+m = 10_000
+n = 100_000
+o = 200_000
+
Benchmark.bm do |x|
- x.report("Struct.new\n") do
- n.times do |i|
+ x.bench("Struct.new\n", :macruby => false) do
+ m.times do |i|
Struct.new("Contact#{i}", :first_name, :last_name, :home_number, :cell_number, :email )
end
end
-end
-n = 100_000
-Benchmark.bm do |x|
- mattetti = Contact.new('Matt', 'Aimonetti', '858-555-5555', '555-444-3333', 'mattaimonetti@gmail.com')
lrz = Contact.new('Laurent', 'Sansonetti', '123-456-5555', '555-444-3333', 'lrz@gmail.com')
+ matt = Contact.new('Matt', 'Aimonetti', '858-555-5555', '555-444-3333', 'mattaimonetti@gmail.com')
- x.report("==\n") do
+
+ x.bench("==\n") do
n.times do
- mattetti == lrz
+ matt == lrz
end
end
- x.report("eql?\n") do
+ x.bench("eql?\n") do
n.times do
- mattetti.eql?(lrz)
+ matt.eql?(lrz)
end
end
- x.report("new instance\n") do
+ x.bench("new instance\n") do
n.times do
Contact.new('Matt', 'Aimonetti', '858-555-5555', '555-444-3333', 'mattaimonetti@gmail.com')
end
end
-end
-
-n = 200_000
-Benchmark.bm do |x|
- matt = Contact.new('Matt', 'Aimonetti', '858-555-5555', '555-444-3333', 'mattaimonetti@gmail.com')
- x.report("attribute reference by string\n") do
- n.times{ matt['home_number'] }
+
+ x.bench("attribute reference by string\n") do
+ o.times{ matt['home_number'] }
end
- x.report("attribute reference by symbol\n") do
- n.times{ matt[:home_number] }
+ x.bench("attribute reference by symbol\n") do
+ o.times{ matt[:home_number] }
end
- x.report("attribute reference by index\n") do
- n.times{ matt[2] }
+ x.bench("attribute reference by index\n") do
+ o.times{ matt[2] }
end
- x.report("each {|obj| block }\n") do
- n.times{ matt.each{|x| x} }
+ x.bench("each {|obj| block }\n") do
+ o.times{ matt.each{|x| x} }
end
- x.report("each_pair {|sym, obj| block }\n") do
- n.times{ matt.each_pair{|name, value| name} }
+ x.bench("each_pair {|sym, obj| block }\n") do
+ o.times{ matt.each_pair{|name, value| name} }
end
- x.report("length\n") do
- n.times{ matt.length }
+ x.bench("length\n") do
+ o.times{ matt.length }
end
- x.report("members\n") do
- n.times{ matt.members }
+ x.bench("members\n") do
+ o.times{ matt.members }
end
- x.report("select{|i| block}\n") do
- n.times{ matt.select{|i| true} }
+ x.bench("select{|i| block}\n") do
+ o.times{ matt.select{|i| true} }
end
- x.report("values_at(indexes)}\n") do
- n.times{ matt.values_at(1,3) }
+ x.bench("values_at(indexes)}\n") do
+ o.times{ matt.values_at(1,3) }
end
- x.report("values_at(negative indexes)}\n") do
- n.times{ matt.values_at(-1,-3) }
+ x.bench("values_at(negative indexes)}\n") do
+ o.times{ matt.values_at(-1,-3) }
end
- x.report("values_at(ranges)}\n") do
- n.times{ matt.values_at(1..3, 2...5) }
+ x.bench("values_at(ranges)}\n") do
+ o.times{ matt.values_at(1..3, 2...5) }
end
-end
-n = 100_000
-Benchmark.bm do |x|
- matt = Contact.new('Matt', 'Aimonetti', '858-555-5555', '555-444-3333', 'mattaimonetti@gmail.com')
+
- x.report("hash\n") do
+ x.bench("hash\n") do
n.times{ matt.hash }
end
- x.report("to_s\n") do
+ x.bench("to_s\n") do
n.times{ matt.to_s }
end
- x.report("to_a\n") do
+ x.bench("to_a\n") do
n.times{ matt.to_a }
- end
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.