Skip to content

Instantly share code, notes, and snippets.

@joshbuddy
Created May 13, 2009 18:37
Show Gist options
  • Save joshbuddy/111195 to your computer and use it in GitHub Desktop.
Save joshbuddy/111195 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'strscan'
s = "this " * 20
puts "with .scan.each"
puts Benchmark.measure {
100000.times do
s.scan(/\w+[ $]/)
end
}
GC.start
puts "with StringScanner"
puts Benchmark.measure {
100000.times do
parts = []
ss = StringScanner.new(s)
while part = ss.scan(/\w+[ $]/)
parts << part
end
end
}
# jruby ss.rb
# with .scan.each
# 2.750000 0.000000 2.750000 ( 2.700000)
# with StringScanner
# 3.110000 0.000000 3.110000 ( 3.110000)
# ruby ss.rb
# with .scan.each
# 2.980000 0.020000 3.000000 ( 3.029403)
# with StringScanner
# 2.800000 0.020000 2.820000 ( 2.839481)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment