Skip to content

Instantly share code, notes, and snippets.

@rlisowski
Created August 21, 2013 08:52
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 rlisowski/6291965 to your computer and use it in GitHub Desktop.
Save rlisowski/6291965 to your computer and use it in GitHub Desktop.
benchmark end_with? against regex
require 'benchmark'
n = 50000
puts "--------"
puts "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
10.times do |iteration|
m = n*iteration
Benchmark.bm(7) do |x|
puts "iteration: #{iteration} times: #{m}"
x.report("end_with? :") do
m.times do
"some question?".end_with?('?')
end
end
x.report("regexp :") do
m.times do
"some question?" =~/\?$/
end
end
end
end
$ rvm ruby-1.9.3-p194,ruby-1.9.3-p429,ruby-1.9.3-p448,ruby-2.0.0-p0,ruby-2.0.0-p195,ruby-2.0.0-p247 do ruby benchmark.rb
--------
1.9.3-p194
user system total real
iteration: 0 times: 0
end_with? : 0.000000 0.000000 0.000000 ( 0.000010)
regexp : 0.000000 0.000000 0.000000 ( 0.000003)
user system total real
iteration: 1 times: 50000
end_with? : 0.010000 0.000000 0.010000 ( 0.013129)
regexp : 0.010000 0.000000 0.010000 ( 0.014243)
user system total real
iteration: 2 times: 100000
end_with? : 0.020000 0.000000 0.020000 ( 0.021342)
regexp : 0.030000 0.000000 0.030000 ( 0.028668)
user system total real
iteration: 3 times: 150000
end_with? : 0.030000 0.000000 0.030000 ( 0.031781)
regexp : 0.040000 0.000000 0.040000 ( 0.042988)
user system total real
iteration: 4 times: 200000
end_with? : 0.050000 0.000000 0.050000 ( 0.042086)
regexp : 0.050000 0.000000 0.050000 ( 0.057055)
user system total real
iteration: 5 times: 250000
end_with? : 0.060000 0.000000 0.060000 ( 0.052622)
regexp : 0.070000 0.000000 0.070000 ( 0.071169)
user system total real
iteration: 6 times: 300000
end_with? : 0.060000 0.000000 0.060000 ( 0.062960)
regexp : 0.090000 0.000000 0.090000 ( 0.085641)
user system total real
iteration: 7 times: 350000
end_with? : 0.070000 0.000000 0.070000 ( 0.074382)
regexp : 0.100000 0.000000 0.100000 ( 0.099469)
user system total real
iteration: 8 times: 400000
end_with? : 0.080000 0.000000 0.080000 ( 0.083798)
regexp : 0.120000 0.000000 0.120000 ( 0.113554)
user system total real
iteration: 9 times: 450000
end_with? : 0.090000 0.000000 0.090000 ( 0.094168)
regexp : 0.130000 0.000000 0.130000 ( 0.127923)
--------
1.9.3-p429
user system total real
iteration: 0 times: 0
end_with? : 0.000000 0.000000 0.000000 ( 0.000008)
regexp : 0.000000 0.000000 0.000000 ( 0.000003)
user system total real
iteration: 1 times: 50000
end_with? : 0.010000 0.000000 0.010000 ( 0.010234)
regexp : 0.020000 0.000000 0.020000 ( 0.013923)
user system total real
iteration: 2 times: 100000
end_with? : 0.020000 0.000000 0.020000 ( 0.020351)
regexp : 0.020000 0.000000 0.020000 ( 0.028117)
user system total real
iteration: 3 times: 150000
end_with? : 0.030000 0.000000 0.030000 ( 0.030229)
regexp : 0.050000 0.000000 0.050000 ( 0.041851)
user system total real
iteration: 4 times: 200000
end_with? : 0.040000 0.000000 0.040000 ( 0.039963)
regexp : 0.050000 0.000000 0.050000 ( 0.054688)
user system total real
iteration: 5 times: 250000
end_with? : 0.050000 0.000000 0.050000 ( 0.050000)
regexp : 0.070000 0.000000 0.070000 ( 0.068536)
user system total real
iteration: 6 times: 300000
end_with? : 0.060000 0.000000 0.060000 ( 0.059744)
regexp : 0.080000 0.000000 0.080000 ( 0.081823)
user system total real
iteration: 7 times: 350000
end_with? : 0.070000 0.000000 0.070000 ( 0.069591)
regexp : 0.100000 0.000000 0.100000 ( 0.095881)
user system total real
iteration: 8 times: 400000
end_with? : 0.070000 0.000000 0.070000 ( 0.079517)
regexp : 0.110000 0.000000 0.110000 ( 0.109295)
user system total real
iteration: 9 times: 450000
end_with? : 0.090000 0.000000 0.090000 ( 0.089339)
regexp : 0.130000 0.000000 0.130000 ( 0.122954)
--------
1.9.3-p448
user system total real
iteration: 0 times: 0
end_with? : 0.000000 0.000000 0.000000 ( 0.000002)
regexp : 0.000000 0.000000 0.000000 ( 0.000001)
user system total real
iteration: 1 times: 50000
end_with? : 0.000000 0.000000 0.000000 ( 0.010075)
regexp : 0.020000 0.000000 0.020000 ( 0.013647)
user system total real
iteration: 2 times: 100000
end_with? : 0.020000 0.000000 0.020000 ( 0.020912)
regexp : 0.030000 0.000000 0.030000 ( 0.027888)
user system total real
iteration: 3 times: 150000
end_with? : 0.030000 0.000000 0.030000 ( 0.030581)
regexp : 0.040000 0.000000 0.040000 ( 0.041456)
user system total real
iteration: 4 times: 200000
end_with? : 0.040000 0.000000 0.040000 ( 0.040652)
regexp : 0.060000 0.000000 0.060000 ( 0.055057)
user system total real
iteration: 5 times: 250000
end_with? : 0.040000 0.000000 0.040000 ( 0.050200)
regexp : 0.080000 0.000000 0.080000 ( 0.068759)
user system total real
iteration: 6 times: 300000
end_with? : 0.060000 0.000000 0.060000 ( 0.060001)
regexp : 0.070000 0.000000 0.070000 ( 0.082836)
user system total real
iteration: 7 times: 350000
end_with? : 0.070000 0.000000 0.070000 ( 0.069922)
regexp : 0.100000 0.000000 0.100000 ( 0.095418)
user system total real
iteration: 8 times: 400000
end_with? : 0.080000 0.000000 0.080000 ( 0.079714)
regexp : 0.110000 0.000000 0.110000 ( 0.109068)
user system total real
iteration: 9 times: 450000
end_with? : 0.090000 0.000000 0.090000 ( 0.089394)
regexp : 0.120000 0.000000 0.120000 ( 0.122516)
--------
2.0.0-p0
user system total real
iteration: 0 times: 0
end_with? : 0.000000 0.000000 0.000000 ( 0.000002)
regexp : 0.000000 0.000000 0.000000 ( 0.000001)
user system total real
iteration: 1 times: 50000
end_with? : 0.010000 0.000000 0.010000 ( 0.011886)
regexp : 0.020000 0.000000 0.020000 ( 0.021306)
user system total real
iteration: 2 times: 100000
end_with? : 0.020000 0.000000 0.020000 ( 0.024294)
regexp : 0.050000 0.000000 0.050000 ( 0.043475)
user system total real
iteration: 3 times: 150000
end_with? : 0.030000 0.000000 0.030000 ( 0.036230)
regexp : 0.070000 0.000000 0.070000 ( 0.063384)
user system total real
iteration: 4 times: 200000
end_with? : 0.040000 0.000000 0.040000 ( 0.048397)
regexp : 0.090000 0.000000 0.090000 ( 0.084185)
user system total real
iteration: 5 times: 250000
end_with? : 0.060000 0.000000 0.060000 ( 0.060162)
regexp : 0.100000 0.000000 0.100000 ( 0.104864)
user system total real
iteration: 6 times: 300000
end_with? : 0.070000 0.000000 0.070000 ( 0.071599)
regexp : 0.130000 0.000000 0.130000 ( 0.125484)
user system total real
iteration: 7 times: 350000
end_with? : 0.080000 0.000000 0.080000 ( 0.083463)
regexp : 0.150000 0.000000 0.150000 ( 0.146646)
user system total real
iteration: 8 times: 400000
end_with? : 0.100000 0.000000 0.100000 ( 0.095071)
regexp : 0.160000 0.000000 0.160000 ( 0.166533)
user system total real
iteration: 9 times: 450000
end_with? : 0.110000 0.000000 0.110000 ( 0.106857)
regexp : 0.190000 0.000000 0.190000 ( 0.187450)
--------
2.0.0-p195
user system total real
iteration: 0 times: 0
end_with? : 0.000000 0.000000 0.000000 ( 0.000002)
regexp : 0.000000 0.000000 0.000000 ( 0.000001)
user system total real
iteration: 1 times: 50000
end_with? : 0.000000 0.000000 0.000000 ( 0.010091)
regexp : 0.020000 0.000000 0.020000 ( 0.017381)
user system total real
iteration: 2 times: 100000
end_with? : 0.020000 0.000000 0.020000 ( 0.020825)
regexp : 0.040000 0.000000 0.040000 ( 0.035695)
user system total real
iteration: 3 times: 150000
end_with? : 0.030000 0.000000 0.030000 ( 0.030835)
regexp : 0.050000 0.000000 0.050000 ( 0.052004)
user system total real
iteration: 4 times: 200000
end_with? : 0.040000 0.000000 0.040000 ( 0.040856)
regexp : 0.070000 0.000000 0.070000 ( 0.068897)
user system total real
iteration: 5 times: 250000
end_with? : 0.050000 0.000000 0.050000 ( 0.050648)
regexp : 0.090000 0.000000 0.090000 ( 0.085919)
user system total real
iteration: 6 times: 300000
end_with? : 0.060000 0.000000 0.060000 ( 0.062273)
regexp : 0.100000 0.000000 0.100000 ( 0.103818)
user system total real
iteration: 7 times: 350000
end_with? : 0.070000 0.000000 0.070000 ( 0.071259)
regexp : 0.120000 0.000000 0.120000 ( 0.119968)
user system total real
iteration: 8 times: 400000
end_with? : 0.080000 0.000000 0.080000 ( 0.081733)
regexp : 0.140000 0.000000 0.140000 ( 0.136598)
user system total real
iteration: 9 times: 450000
end_with? : 0.090000 0.000000 0.090000 ( 0.090097)
regexp : 0.150000 0.000000 0.150000 ( 0.153518)
--------
2.0.0-p247
user system total real
iteration: 0 times: 0
end_with? : 0.000000 0.000000 0.000000 ( 0.000002)
regexp : 0.000000 0.000000 0.000000 ( 0.000001)
user system total real
iteration: 1 times: 50000
end_with? : 0.010000 0.000000 0.010000 ( 0.010036)
regexp : 0.020000 0.000000 0.020000 ( 0.017158)
user system total real
iteration: 2 times: 100000
end_with? : 0.020000 0.000000 0.020000 ( 0.020734)
regexp : 0.030000 0.000000 0.030000 ( 0.034852)
user system total real
iteration: 3 times: 150000
end_with? : 0.030000 0.000000 0.030000 ( 0.030612)
regexp : 0.050000 0.000000 0.050000 ( 0.051574)
user system total real
iteration: 4 times: 200000
end_with? : 0.040000 0.000000 0.040000 ( 0.040782)
regexp : 0.070000 0.000000 0.070000 ( 0.068173)
user system total real
iteration: 5 times: 250000
end_with? : 0.050000 0.000000 0.050000 ( 0.050342)
regexp : 0.090000 0.000000 0.090000 ( 0.085228)
user system total real
iteration: 6 times: 300000
end_with? : 0.060000 0.000000 0.060000 ( 0.059976)
regexp : 0.100000 0.000000 0.100000 ( 0.102066)
user system total real
iteration: 7 times: 350000
end_with? : 0.070000 0.000000 0.070000 ( 0.069760)
regexp : 0.120000 0.000000 0.120000 ( 0.118850)
user system total real
iteration: 8 times: 400000
end_with? : 0.080000 0.000000 0.080000 ( 0.079716)
regexp : 0.130000 0.000000 0.130000 ( 0.135446)
user system total real
iteration: 9 times: 450000
end_with? : 0.090000 0.000000 0.090000 ( 0.090365)
regexp : 0.160000 0.000000 0.160000 ( 0.151947)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment