Skip to content

Instantly share code, notes, and snippets.

@avescodes
Created October 29, 2008 03:47
Show Gist options
  • Save avescodes/20616 to your computer and use it in GitHub Desktop.
Save avescodes/20616 to your computer and use it in GitHub Desktop.
require 'benchmark'
str = "1 2 3 4-5 6 7 8-9"
Benchmark.bm do |bm|
bm.report("split: ") {10000.times do hash = Hash.new(0); str.split.each { |m| hash[m] += 1}; end }
bm.report("scan: (\\w+) ") { 10000.times do hash = Hash.new(0); str.scan(/\w+/m) { |m| hash[m] += 1} end }
bm.report("scan: (\w+(-\w+)?) ") { 10000.times do hash = Hash.new(0); str.scan(/(\w+(-\w+)?)/m) { |m| hash[m] += 1} end }
end
## Native environment tests - 1.8.7
#Creating one hash and clear it: (hash.clear instead of hash = Hash.new(0))
# user system total real
# split: 0.490000 0.150000 0.640000 ( 0.656165)
# scan: (\w+) 0.800000 0.180000 0.980000 ( 1.003529)
# scan: (w+(-w+)?) 1.390000 0.340000 1.730000 ( 1.745792)
#Creating a new hash every time:
# user system total real
# split: 0.470000 0.140000 0.610000 ( 0.643760)
# scan: (\w+) 0.800000 0.180000 0.980000 ( 0.989383)
# scan: (w+(-w+)?) 1.170000 0.260000 1.430000 ( 1.457280)
## Variety tests by Stef Penner
#mbp:rubinius stefan$ ruby -v
# -> ruby 1.8.7 (2008-06-20 patchlevel 22) [i686-darwin9.3.0]
#mbp:rubinius stefan$ macruby -v
# -> MacRuby version 0.3 (ruby 1.9.0 2008-06-03) [universal-darwin9.0]
#mbp:rubinius stefan$ jruby -v
# -> ruby 1.8.6 (2008-06-22 rev 6555) [i386-jruby1.1.1]
#mbp:rubinius stefan$ rbx -v
# -> rubinius 0.9.0 (ruby 1.8.6 compatible) (8038487c4) (10/19/2008) [i686-apple-darwin9.5.0]
# $ rubinous regx.rb
# user system total real
# split: 1.422384 0.000000 1.422384 ( 1.422366)
# scan: (\w+) 1.458300 0.000000 1.458300 ( 1.458299)
# scan: (w+(-w+)?) 2.127930 0.000000 2.127930 ( 2.127929)
# $ ruby regx.rb
# user system total real
# split: 0.410000 0.140000 0.550000 ( 0.559599)
# scan: (\w+) 0.670000 0.180000 0.850000 ( 0.862585)
# scan: (w+(-w+)?) 0.990000 0.270000 1.260000 ( 1.268065)
# $ ruby1.9 regx.rb
# user system total real
# split: 0.090000 0.000000 0.090000 ( 0.096752)
# scan: (\w+) 0.170000 0.000000 0.170000 ( 0.164321)
# scan: (w+(-w+)?) 0.280000 0.000000 0.280000 ( 0.291374)
# $ macruby regx.rb
# user system total real
# split: 0.440000 0.030000 0.470000 ( 0.490660)
# scan: (\w+) 4.310000 0.050000 4.360000 ( 4.449849)
# scan: (w+(-w+)?) 4.380000 0.040000 4.420000 ( 4.503897)
# $ jruby regx.rb
# user system total real
# split: 0.456000 0.000000 0.456000 ( 0.456000)
# scan: (\w+) 0.261000 0.000000 0.261000 ( 0.260000)
# scan: (w+(-w+)?) 0.369000 0.000000 0.369000 ( 0.369000)
# $jruby 1.1.3 regx.rb
# user system total real
# split: 0.235000 0.000000 0.235000 ( 0.234993)
# scan: (\w+) 0.228000 0.000000 0.228000 ( 0.228318)
# scan: (w+(-w+)?) 0.329000 0.000000 0.329000 ( 0.328884)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment