Skip to content

Instantly share code, notes, and snippets.

@mpugach
Last active June 1, 2016 10:26
Show Gist options
  • Select an option

  • Save mpugach/13e6c4d89cad25cc60ead2eb2909536f to your computer and use it in GitHub Desktop.

Select an option

Save mpugach/13e6c4d89cad25cc60ead2eb2909536f to your computer and use it in GitHub Desktop.
Benchmark for executing logic from If, Case and Hash
ruby-2.3-head-railsexpress
#!/usr/bin/env ruby
require 'benchmark'
require 'ruby-prof'
count = 1000
array = (1..1_000).map { rand }
variants = [true, false]
Benchmark.bmbm(5) do |bm|
bm.report('if') do
variants.cycle(count) do |variant|
if variant
array.sort
else
array.sort
end
end
end
bm.report('case') do
variants.cycle(count) do |variant|
case variant
when true
array.sort
when false
array.sort
end
end
end
bm.report('hash') do
variants.cycle(count) do |variant|
{
true => -> { array.sort },
false => -> { array.sort }
}[variant].call
end
end
end
[RubyProf::GC_TIME, RubyProf::MEMORY].each do |mode|
RubyProf.measure_mode = mode
puts "\n\nIF\n"
RubyProf.start
variants.cycle(count) do |variant|
if variant
array.sort
else
array.sort
end
end
RubyProf::FlatPrinter.new(RubyProf.stop).print(STDOUT)
puts "\n\nCASE\n"
RubyProf.start
variants.cycle(count) do |variant|
case variant
when true
array.sort
when false
array.sort
end
end
RubyProf::FlatPrinter.new(RubyProf.stop).print(STDOUT)
puts "\n\nHASH\n"
RubyProf.start
variants.cycle(count) do |variant|
{
true => -> { array.sort },
false => -> { array.sort }
}[variant].call
end
RubyProf::FlatPrinter.new(RubyProf.stop).print(STDOUT)
end
# RESULTS
# Rehearsal -----------------------------------------
# if 1.500000 0.020000 1.520000 ( 1.537125)
# case 1.530000 0.010000 1.540000 ( 1.561478)
# hash 1.540000 0.020000 1.560000 ( 1.575083)
# -------------------------------- total: 4.620000sec
# user system total real
# if 1.510000 0.010000 1.520000 ( 1.557586)
# case 1.540000 0.010000 1.550000 ( 1.587463)
# hash 1.560000 0.000000 1.560000 ( 1.578515)
# IF
# Measure Mode: gc_time
# Thread ID: 70170378975240
# Fiber ID: 70170375037540
# Total: 0.000497
# Sort by: self_time
# %self total self wait child calls name
# 100.00 0.000 0.000 0.000 0.000 2000 Array#sort
# 0.00 0.000 0.000 0.000 0.000 19648000 Float#<=>
# 0.00 0.000 0.000 0.000 0.000 1 Array#cycle
# 0.00 0.000 0.000 0.000 0.000 1 Global#[No method]
# * indicates recursively called methods
# CASE
# Measure Mode: gc_time
# Thread ID: 70170378975240
# Fiber ID: 70170375037540
# Total: 0.001162
# Sort by: self_time
# %self total self wait child calls name
# 100.00 0.001 0.001 0.000 0.000 2000 Array#sort
# 0.00 0.000 0.000 0.000 0.000 19648000 Float#<=>
# 0.00 0.001 0.000 0.000 0.001 1 Array#cycle
# 0.00 0.001 0.000 0.000 0.001 1 Global#[No method]
# * indicates recursively called methods
# HASH
# Measure Mode: gc_time
# Thread ID: 70170378975240
# Fiber ID: 70170375037540
# Total: 0.003833
# Sort by: self_time
# %self total self wait child calls name
# 67.41 0.004 0.003 0.000 0.001 1 Array#cycle
# 32.59 0.001 0.001 0.000 0.000 2000 Array#sort
# 0.00 0.000 0.000 0.000 0.000 19648000 Float#<=>
# 0.00 0.004 0.000 0.000 0.004 1 Global#[No method]
# * indicates recursively called methods
# IF
# Measure Mode: memory
# Thread ID: 70170378975240
# Fiber ID: 70170375037540
# Total: 32002.359375
# Sort by: self_time
# %self total self wait child calls name
# 99.99 32000.781 32000.391 0.000 0.391 2000 Array#sort
# 0.00 32002.359 1.188 0.000 32001.172 1 Global#[No method]
# 0.00 0.391 0.391 0.000 0.000 19648000 Float#<=>
# 0.00 32001.172 0.391 0.000 32000.781 1 Array#cycle
# * indicates recursively called methods
# CASE
# Measure Mode: memory
# Thread ID: 70170378975240
# Fiber ID: 70170375037540
# Total: 32002.359375
# Sort by: self_time
# %self total self wait child calls name
# 99.99 32000.781 32000.391 0.000 0.391 2000 Array#sort
# 0.00 32002.359 1.188 0.000 32001.172 1 Global#[No method]
# 0.00 0.391 0.391 0.000 0.000 19648000 Float#<=>
# 0.00 32001.172 0.391 0.000 32000.781 1 Array#cycle
# * indicates recursively called methods
# HASH
# Measure Mode: memory
# Thread ID: 70170378975240
# Fiber ID: 70170375037540
# Total: 32721.187500
# Sort by: self_time
# %self total self wait child calls name
# 97.80 32000.781 32000.391 0.000 0.391 2000 Array#sort
# 2.20 32720.000 719.219 0.000 32000.781 1 Array#cycle
# 0.00 32721.188 1.188 0.000 32720.000 1 Global#[No method]
# 0.00 0.391 0.391 0.000 0.000 19648000 Float#<=>
# * indicates recursively called methods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment