Skip to content

Instantly share code, notes, and snippets.

@evanphx
Created January 13, 2015 18:24
Show Gist options
  • Save evanphx/316153c1c8b813490997 to your computer and use it in GitHub Desktop.
Save evanphx/316153c1c8b813490997 to your computer and use it in GitHub Desktop.
micro benchmarks with benchmark-ips
require 'benchmark'
require 'benchmark/ips'
outer_b = 0
Benchmark.ips do |x|
outer_a = 0
x.report "inner a block" do
inner_a = 1
end
x.report "outer a block" do
outer_a = 1
end
x.report "outer b block" do
outer_b = 1
end
x.report "inner a block var" do |t|
t.times { inner_a = 1 }
end
x.report "outer a block var" do |t|
t.times { outer_a = 1 }
end
x.report "outer b block var" do |t|
t.times { outer_b = 1 }
end
x.report "inner a block var while" do |t|
i = 0
while i < t
inner_a = 1
i += 1
end
end
x.report "inner a string", "inner_a = 1"
end
zero :: ~/scratch> ruby ko1.rb
Calculating -------------------------------------
inner a block 129.411k i/100ms
outer a block 132.007k i/100ms
outer b block 127.053k i/100ms
inner a block var 119.593k i/100ms
outer a block var 116.482k i/100ms
outer b block var 120.291k i/100ms
inner a block var while
129.009k i/100ms
inner a string 158.832k i/100ms
-------------------------------------------------
inner a block 8.877M (±15.6%) i/s - 43.353M
outer a block 8.765M (±14.6%) i/s - 42.902M
outer b block 8.817M (±14.0%) i/s - 43.198M
inner a block var 18.502M (±19.2%) i/s - 88.618M
outer a block var 19.170M (±17.7%) i/s - 92.370M
outer b block var 18.242M (±18.0%) i/s - 87.692M
inner a block var while
39.581M (±19.3%) i/s - 188.869M
inner a string 39.627M (±18.5%) i/s - 189.963M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment