Skip to content

Instantly share code, notes, and snippets.

@mcansky
Last active December 11, 2015 19:28
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 mcansky/4648380 to your computer and use it in GitHub Desktop.
Save mcansky/4648380 to your computer and use it in GitHub Desktop.
require 'benchmark'
n = 500
archs = ["i486", "ia64"]
archs.each do |arch|
Benchmark.bm(7) do |x|
x.report("case : #{arch}") { n.times { case arch when /i\d86/ then "i386" else arch end }}
x.report("if (match): #{arch}") { n.times { if (arch.match(/i\d86/)) then "i386" else arch end }}
x.report("? (match) : #{arch}") { n.times { arch.match(/i\d86/) ? "i386" : arch }}
x.report("if (=~) : #{arch}") { n.times { if (arch =~ (/i\d86/)) then "i386" else arch end }}
x.report("? (=~) : #{arch}") { n.times { arch =~ (/i\d86/) ? "i386" : arch }}
end
end
# user system total real
# case : i486 0.000000 0.000000 0.000000 ( 0.000257)
# if (match): i486 0.000000 0.000000 0.000000 ( 0.000900)
# ? (match) : i486 0.000000 0.000000 0.000000 ( 0.000584)
# if (=~) : i486 0.000000 0.000000 0.000000 ( 0.000220)
# ? (=~) : i486 0.000000 0.000000 0.000000 ( 0.000228)
# user system total real
# case : ia64 0.000000 0.000000 0.000000 ( 0.000246)
# if (match): ia64 0.000000 0.000000 0.000000 ( 0.000305)
# ? (match) : ia64 0.000000 0.000000 0.000000 ( 0.000266)
# if (=~) : ia64 0.000000 0.000000 0.000000 ( 0.000209)
# ? (=~) : ia64 0.000000 0.000000 0.000000 ( 0.000229)
@mcansky
Copy link
Author

mcansky commented Jan 27, 2013

even with n = 50000 we get something similar

#              user     system      total        real
# case      : i486  0.020000   0.000000   0.020000 (  0.020706)
# if (match): i486  0.070000   0.000000   0.070000 (  0.068858)
# ? (match) : i486  0.060000   0.000000   0.060000 (  0.068658)
# if (=~)   : i486  0.020000   0.000000   0.020000 (  0.018694)
# ? (=~)    : i486  0.020000   0.000000   0.020000 (  0.017124)
#              user     system      total        real
# case      : ia64  0.020000   0.000000   0.020000 (  0.018824)
# if (match): ia64  0.020000   0.000000   0.020000 (  0.024929)
# ? (match) : ia64  0.030000   0.000000   0.030000 (  0.024002)
# if (=~)   : ia64  0.020000   0.000000   0.020000 (  0.018476)
# ? (=~)    : ia64  0.010000   0.000000   0.010000 (  0.017843)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment