Skip to content

Instantly share code, notes, and snippets.

@makaroni4
Created December 2, 2016 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makaroni4/164620e5301949002317ec014c6310d2 to your computer and use it in GitHub Desktop.
Save makaroni4/164620e5301949002317ec014c6310d2 to your computer and use it in GitHub Desktop.
Code for blog post "Benchmarking with Ruby"
require 'benchmark'
require 'benchmark/ips'
Benchmark.ips do |r|
r.report("+ ") do
42 + 42
end
r.report("* ") do
42 * 42
end
end
# =>Calculating -
# => + 92249 i/100ms
# => * 91950 i/100ms
# =>-
# => + 6852082.4 (±6.8%) i/s - 34039881 in 4.998483s
# => * 6280292.3 (±5.5%) i/s - 31263000 in 4.996057s
require 'benchmark'
Benchmark.bm do |r|
N = 100000
r.report("+= ") do
s = ""
N.times { s += "1" }
end
r.report("<< ") do
s = ""
N.times { s << "1" }
end
end
# => user system total real
# => += 1.060000 0.340000 1.400000 ( 1.405588)
# => << 0.030000 0.000000 0.030000 ( 0.025614)
require 'benchmark'
require 'benchmark/ips'
Benchmark.ips do |r|
N = 100000
r.report("+= ") do
s = ""
N.times { s += "." }
end
r.report("<< ") do
s = ""
N.times { s << "." }
end
end
# => Calculating -
# => += 1 i/100ms
# => << 3 i/100ms
# =>-
# => += 0.2 (±0.0%) i/s - 2 in 8.508143s
# => << 33.9 (±3.0%) i/s - 171 in 5.054061s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment