Skip to content

Instantly share code, notes, and snippets.

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 boddhisattva/ff4b03bd48d4356753537690cb118c47 to your computer and use it in GitHub Desktop.
Save boddhisattva/ff4b03bd48d4356753537690cb118c47 to your computer and use it in GitHub Desktop.
Benchmark comparison scripts wrt the bob exercise
SILENT = /\A\s*\z/
SILENT2 = ->(remark) { remark.strip.empty? }
def hey_fast(remark)
case remark
when SILENT
'Fine. Be that way!'
else
"Whatever."
end
end
def hey_slow(remark)
case remark
when SILENT2
'Fine. Be that way!'
else
"Whatever."
end
end
Benchmark.ips do |x|
x.report("fast code description") { hey_fast(" ") }
x.report("slow code description") { hey_slow(" ") }
x.compare!
end
SILENT = ->(remark) { remark.end_with?('?') }
SILENT2 = /\?\z/
def hey_fast(remark)
case remark
when SILENT
'Sure.'
else
"Whatever."
end
end
def hey_slow(remark)
case remark
when SILENT2
'Sure.'
else
"Whatever."
end
end
Benchmark.ips do |x|
x.report("fast code description") { hey_fast("You are, what, like 15?") }
x.report("slow code description") { hey_slow("You are, what, like 15?") }
x.compare!
end
Warming up --------------------------------------
fast code description
79.607k i/100ms
slow code description
37.594k i/100ms
Calculating -------------------------------------
fast code description
2.539M (± 8.7%) i/s - 12.658M
slow code description
718.586k (± 7.6%) i/s - 3.571M
Comparison:
fast code description: 2538652.8 i/s
slow code description: 718585.6 i/s - 3.53x slower
=> #<Benchmark::IPS::Report:0x007fa741959328 @entries=[#<Benchmark::IPS::Report::Entry:0x007fa741b433c8 @label="fast code description", @microseconds=5027441.024780273, @iterations=12657513, @ips=2538652.765822625, @ips_sd=220997, @measurement_cycle=79607, @show_total_time=false>, #<Benchmark::IPS::Report::Entry:0x007fa741b0b928 @label="slow code description", @microseconds=5000115.633010864, @iterations=3571430, @ips=718585.6422104437, @ips_sd=54368, @measurement_cycle=37594, @show_total_time=false>], @data=nil>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment