Skip to content

Instantly share code, notes, and snippets.

@matisojka
Created November 27, 2013 08:40
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 matisojka/7672557 to your computer and use it in GitHub Desktop.
Save matisojka/7672557 to your computer and use it in GitHub Desktop.
First string character comparison benchmark
require 'benchmark'
name = 'my_cool_string'
Benchmark.bmbm(10) do |x|
x.report('name[0] hit') do
1_000_000.times { name[0] == 'm' }
end
x.report('name[0] miss') do
1_000_000.times { name[0] == 'y' }
end
x.report('name.start_with? hit') do
1_000_000.times { name.start_with?('m') }
end
x.report('name.start_with? miss') do
1_000_000.times { name.start_with?('y') }
end
x.report('name.match hit') do
1_000_000.times { name.match(/^m/) }
end
x.report('name.match miss') do
1_000_000.times { name.match(/^y/) }
end
x.report('name =~ hit') do
1_000_000.times { name =~ /^m/ }
end
x.report('name =~ miss') do
1_000_000.times { name =~ /^y/ }
end
end
MacBook Pro 15'', Mid 2010
2.66 GHz Intel Core i7
8 GB 1067 MHz DDR3
ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin12.2.0]
Rehearsal ---------------------------------------------------------
name[0] hit 0.980000 0.010000 0.990000 ( 0.983280)
name[0] miss 1.050000 0.000000 1.050000 ( 1.054768)
name.start_with? hit 0.600000 0.000000 0.600000 ( 0.624704)
name.start_with? miss 0.560000 0.000000 0.560000 ( 0.556793)
name.match hit 2.380000 0.020000 2.400000 ( 2.408706)
name.match miss 0.610000 0.000000 0.610000 ( 0.611798)
name =~ hit 0.630000 0.000000 0.630000 ( 0.637947)
name =~ miss 0.470000 0.010000 0.480000 ( 0.466624)
------------------------------------------------ total: 7.320000sec
user system total real
name[0] hit 0.960000 0.000000 0.960000 ( 0.957911)
name[0] miss 0.920000 0.000000 0.920000 ( 0.919704)
name.start_with? hit 0.530000 0.000000 0.530000 ( 0.539560)
name.start_with? miss 0.540000 0.000000 0.540000 ( 0.545029)
name.match hit 2.220000 0.020000 2.240000 ( 2.241739)
name.match miss 0.680000 0.000000 0.680000 ( 0.687998)
name =~ hit 0.660000 0.000000 0.660000 ( 0.669767)
name =~ miss 0.510000 0.000000 0.510000 ( 0.508524)
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.3.0]
Rehearsal ---------------------------------------------------------
name[0] hit 1.140000 0.010000 1.150000 ( 1.143242)
name[0] miss 1.130000 0.000000 1.130000 ( 1.132830)
name.start_with? hit 0.590000 0.000000 0.590000 ( 0.599413)
name.start_with? miss 0.610000 0.000000 0.610000 ( 0.612994)
name.match hit 2.720000 0.040000 2.760000 ( 2.770379)
name.match miss 0.900000 0.000000 0.900000 ( 0.899769)
name =~ hit 0.830000 0.000000 0.830000 ( 0.840448)
name =~ miss 0.720000 0.000000 0.720000 ( 0.736234)
------------------------------------------------ total: 8.690000sec
user system total real
name[0] hit 1.090000 0.000000 1.090000 ( 1.098711)
name[0] miss 1.110000 0.010000 1.120000 ( 1.115329)
name.start_with? hit 0.590000 0.000000 0.590000 ( 0.598362)
name.start_with? miss 0.590000 0.000000 0.590000 ( 0.590486)
name.match hit 2.460000 0.020000 2.480000 ( 2.486387)
name.match miss 0.880000 0.000000 0.880000 ( 0.878244)
name =~ hit 1.000000 0.010000 1.010000 ( 1.023415)
name =~ miss 0.710000 0.000000 0.710000 ( 0.748270)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment