Skip to content

Instantly share code, notes, and snippets.

@dominikh
Created October 12, 2009 01:15
Show Gist options
  • Save dominikh/208025 to your computer and use it in GitHub Desktop.
Save dominikh/208025 to your computer and use it in GitHub Desktop.
require 'benchmark'
n = 1_000_000
s = "a b c d e f g h"
Benchmark.bmbm do |x|
x.report("Scan (regexp)") { n.times { s.scan(/h/).empty? } }
x.report("Scan (string)") { n.times { s.scan("h").empty? } }
x.report("Include") { n.times { s.include?("h") } }
x.report("=~") { n.times { s =~ /h/} }
x.report("Scan (regexp) (not findable)") { n.times { s.scan(/i/).empty? } }
x.report("Scan (string) (not findable)") { n.times { s.scan("i").empty? } }
x.report("Include (not findable)") { n.times { s.include?("i") } }
x.report("=~ (not findable)") { n.times { s =~ /i/} }
end
# Rehearsal ----------------------------------------------------------------
# Scan (regexp) 4.710000 0.010000 4.720000 ( 4.726882)
# Scan (string) 6.420000 0.020000 6.440000 ( 6.445207)
# Include 0.870000 0.000000 0.870000 ( 0.866947)
# =~ 1.100000 0.000000 1.100000 ( 1.100991)
#
# Scan (regexp) (not findable) 1.750000 0.010000 1.760000 ( 1.758479)
# Scan (string) (not findable) 2.620000 0.000000 2.620000 ( 2.642941)
# Include (not findable) 0.830000 0.000000 0.830000 ( 0.840811)
# =~ (not findable) 1.040000 0.010000 1.050000 ( 1.061319)
# ------------------------------------------------------ total: 19.390000sec
# user system total real
# Scan (regexp) 5.000000 0.010000 5.010000 ( 5.071590)
# Scan (string) 6.420000 0.020000 6.440000 ( 6.509148)
# Include 0.870000 0.000000 0.870000 ( 0.884166)
# =~ 1.310000 0.000000 1.310000 ( 1.321260)
#
# Scan (regexp) (not findable) 1.610000 0.000000 1.610000 ( 1.618463)
# Scan (string) (not findable) 2.780000 0.000000 2.780000 ( 2.789644)
# Include (not findable) 0.830000 0.000000 0.830000 ( 0.831013)
# =~ (not findable) 0.980000 0.010000 0.990000 ( 0.985075)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment