Skip to content

Instantly share code, notes, and snippets.

@rklemme
Created June 24, 2010 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rklemme/451622 to your computer and use it in GitHub Desktop.
Save rklemme/451622 to your computer and use it in GitHub Desktop.
18:00:04 Temp$ allruby sc.rb
CYGWIN_NT-5.1 padrklemme1 1.7.5(0.225/5/3) 2010-04-12 19:07 i686 Cygwin
========================================
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
Rehearsal ----------------------------------------------
scan 2.766000 0.000000 2.766000 ( 2.786000)
scan ++ 4.656000 0.000000 4.656000 ( 4.668000)
scan re 2.688000 0.000000 2.688000 ( 2.696000)
scan re ++ 4.531000 0.000000 4.531000 ( 4.547000)
while 1.094000 0.000000 1.094000 ( 1.135000)
------------------------------------ total: 15.735000sec
user system total real
scan 2.875000 0.000000 2.875000 ( 2.888000)
scan ++ 4.640000 0.000000 4.640000 ( 4.648000)
scan re 2.672000 0.000000 2.672000 ( 2.698000)
scan re ++ 4.578000 0.000000 4.578000 ( 4.593000)
while 0.985000 0.000000 0.985000 ( 1.029000)
========================================
ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-cygwin]
Rehearsal ----------------------------------------------
scan 1.860000 0.000000 1.860000 ( 1.867000)
scan ++ 1.765000 0.000000 1.765000 ( 1.767000)
scan re 1.719000 0.000000 1.719000 ( 1.731000)
scan re ++ 1.641000 0.000000 1.641000 ( 1.643000)
while 0.578000 0.000000 0.578000 ( 0.578000)
------------------------------------- total: 7.563000sec
user system total real
scan 1.969000 0.000000 1.969000 ( 2.010000)
scan ++ 1.797000 0.000000 1.797000 ( 1.835000)
scan re 1.718000 0.000000 1.718000 ( 1.710000)
scan re ++ 1.844000 0.000000 1.844000 ( 1.863000)
while 0.813000 0.000000 0.813000 ( 0.817000)
========================================
jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (Java HotSpot(TM) Client VM 1.6.0_20) [x86-java]
Rehearsal ----------------------------------------------
scan 0.750000 0.000000 0.750000 ( 0.703000)
scan ++ 0.906000 0.000000 0.906000 ( 0.906000)
scan re 0.516000 0.000000 0.516000 ( 0.516000)
scan re ++ 0.797000 0.000000 0.797000 ( 0.812000)
while 0.453000 0.000000 0.453000 ( 0.453000)
------------------------------------- total: 3.422000sec
user system total real
scan 0.563000 0.000000 0.563000 ( 0.563000)
scan ++ 0.859000 0.000000 0.859000 ( 0.859000)
scan re 0.485000 0.000000 0.485000 ( 0.485000)
scan re ++ 0.797000 0.000000 0.797000 ( 0.797000)
while 0.437000 0.000000 0.437000 ( 0.437000)
18:01:21 Temp$
require 'benchmark'
TIMES = 100_000
s = "you like to play with your yo-yo"
Benchmark.bmbm do |x|
x.report("scan") do
TIMES.times do
count = s.scan("yo").size
raise count unless count == 4
end
end
x.report("scan ++") do
TIMES.times do
count = 0
s.scan("yo") { count += 1 }
raise count unless count == 4
end
end
x.report("scan re") do
TIMES.times do
count = s.scan(/yo/).size
raise count unless count == 4
end
end
x.report("scan re ++") do
TIMES.times do
count = 0
s.scan(/yo/) { count += 1 }
raise count unless count == 4
end
end
x.report("while") do
TIMES.times do
len = "yo".length
index = -len
count = 0
while (index = s.index("yo", index + len))
count += 1
end
count
raise count unless count == 4
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment