Skip to content

Instantly share code, notes, and snippets.

@manveru
Forked from rklemme/sc.rb
Created June 24, 2010 17:04
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 manveru/451675 to your computer and use it in GitHub Desktop.
Save manveru/451675 to your computer and use it in GitHub Desktop.
iota % rvm ruby sc.rb
info: jruby-1.5.1: jruby 1.5.1 (ruby 1.9.2dev trunk 24787) (2010-06-06 f3a3480) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_20) [amd64-java]
Rehearsal ----------------------------------------------
scan 5.973000 0.000000 5.973000 ( 5.928000)
scan ++ 10.103000 0.000000 10.103000 ( 10.104000)
scan re 5.443000 0.000000 5.443000 ( 5.443000)
scan re ++ 10.329000 0.000000 10.329000 ( 10.329000)
while 8.482000 0.000000 8.482000 ( 8.482000)
strscan 9.329000 0.000000 9.329000 ( 9.329000)
------------------------------------ total: 49.659000sec
user system total real
scan 5.473000 0.000000 5.473000 ( 5.473000)
scan ++ 10.323000 0.000000 10.323000 ( 10.323000)
scan re 5.466000 0.000000 5.466000 ( 5.466000)
scan re ++ 10.325000 0.000000 10.325000 ( 10.325000)
while 9.584000 0.000000 9.584000 ( 9.584000)
strscan 9.312000 0.000000 9.312000 ( 9.312000)
info: rbx-1.0.1-20100603: rubinius 1.0.1 (1.8.7 release 2010-06-03 JI) [x86_64-unknown-linux-gnu]
Rehearsal ----------------------------------------------
scan 50.716875 0.000000 50.716875 ( 50.716882)
scan ++ 34.023308 0.000000 34.023308 ( 34.023325)
scan re 49.369605 0.000000 49.369605 ( 49.369626)
scan re ++ 32.813090 0.000000 32.813090 ( 32.813111)
while 16.316727 0.000000 16.316727 ( 16.316749)
strscan 145.435467 0.000000 145.435467 (145.435483)
----------------------------------- total: 328.675072sec
user system total real
scan 51.682330 0.000000 51.682330 ( 51.682343)
scan ++ 34.472589 0.000000 34.472589 ( 34.472601)
scan re 51.113310 0.000000 51.113310 ( 51.113323)
scan re ++ 33.103077 0.000000 33.103077 ( 33.103090)
while 16.987662 0.000000 16.987662 ( 16.987675)
strscan 151.208399 0.000000 151.208399 (151.208400)
info: ruby-1.8.6-p399: ruby 1.8.6 (2010-02-05 patchlevel 399) [x86_64-linux]
Rehearsal ----------------------------------------------
scan 22.760000 0.050000 22.810000 ( 22.971944)
scan ++ 43.000000 0.070000 43.070000 ( 43.424382)
scan re 23.440000 0.010000 23.450000 ( 23.539722)
scan re ++ 42.770000 0.050000 42.820000 ( 42.997112)
while 23.110000 0.050000 23.160000 ( 23.280662)
strscan 24.910000 0.000000 24.910000 ( 25.118870)
----------------------------------- total: 180.220000sec
user system total real
scan 21.940000 0.000000 21.940000 ( 22.060856)
scan ++ 41.280000 0.010000 41.290000 ( 41.609093)
scan re 22.050000 0.020000 22.070000 ( 22.234635)
scan re ++ 41.410000 0.010000 41.420000 ( 41.745685)
while 21.730000 0.000000 21.730000 ( 21.872516)
strscan 24.930000 0.010000 24.940000 ( 25.153422)
info: ruby-1.8.7-p174: ruby 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux]
Rehearsal ----------------------------------------------
scan 21.260000 0.020000 21.280000 ( 21.356280)
scan ++ 41.420000 0.020000 41.440000 ( 41.756201)
scan re 21.120000 0.000000 21.120000 ( 21.180461)
scan re ++ 41.360000 0.010000 41.370000 ( 41.491708)
while 21.730000 0.000000 21.730000 ( 21.790211)
strscan 23.300000 0.010000 23.310000 ( 23.372158)
----------------------------------- total: 170.250000sec
user system total real
scan 21.180000 0.000000 21.180000 ( 21.254311)
scan ++ 41.210000 0.020000 41.230000 ( 41.369151)
scan re 21.150000 0.000000 21.150000 ( 21.221280)
scan re ++ 41.290000 0.010000 41.300000 ( 41.419465)
while 21.530000 0.010000 21.540000 ( 21.603315)
strscan 23.090000 0.000000 23.090000 ( 23.161337)
info: ruby-1.9.1-p378: ruby 1.9.1p378 (2010-01-10 revision 26273) [x86_64-linux]
Rehearsal ----------------------------------------------
scan 13.250000 0.000000 13.250000 ( 13.284770)
scan ++ 16.680000 0.000000 16.680000 ( 16.721028)
scan re 13.360000 0.000000 13.360000 ( 13.397933)
scan re ++ 17.110000 0.010000 17.120000 ( 17.165702)
while 11.950000 0.000000 11.950000 ( 11.978350)
strscan 12.900000 0.010000 12.910000 ( 12.945011)
------------------------------------ total: 85.270000sec
user system total real
scan 13.250000 0.000000 13.250000 ( 13.289802)
scan ++ 16.730000 0.000000 16.730000 ( 16.770702)
scan re 13.270000 0.010000 13.280000 ( 13.310795)
scan re ++ 17.180000 0.010000 17.190000 ( 17.238228)
while 12.000000 0.010000 12.010000 ( 12.032856)
strscan 12.910000 0.010000 12.920000 ( 12.958529)
info: ruby-1.9.2-preview3: ruby 1.9.2dev (2010-05-31 revision 28117) [x86_64-linux]
Rehearsal ----------------------------------------------
scan 11.960000 0.000000 11.960000 ( 12.020126)
scan ++ 15.710000 0.000000 15.710000 ( 15.755604)
scan re 12.000000 0.010000 12.010000 ( 12.036307)
scan re ++ 15.490000 0.000000 15.490000 ( 15.539218)
while 8.780000 0.010000 8.790000 ( 8.799675)
strscan 13.610000 0.000000 13.610000 ( 13.654532)
------------------------------------ total: 77.570000sec
user system total real
scan 11.930000 0.010000 11.940000 ( 11.957146)
scan ++ 15.740000 0.000000 15.740000 ( 15.786451)
scan re 12.020000 0.000000 12.020000 ( 12.046305)
scan re ++ 15.480000 0.010000 15.490000 ( 15.535832)
while 8.810000 0.010000 8.820000 ( 8.837152)
strscan 13.540000 0.010000 13.550000 ( 13.587468)
require 'benchmark'
require 'strscan'
TIMES = 100_000
s = "you like to play with your yo-yo" * 100
def check(count)
raise "Wrong count: #{count}" unless count == 400
end
Benchmark.bmbm do |x|
x.report("scan") do
TIMES.times do
count = s.scan("yo").size
check count
end
end
x.report("scan ++") do
TIMES.times do
count = 0
s.scan("yo") { count += 1 }
check count
end
end
x.report("scan re") do
TIMES.times do
count = s.scan(/yo/).size
check count
end
end
x.report("scan re ++") do
TIMES.times do
count = 0
s.scan(/yo/) { count += 1 }
check count
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
check count
end
end
x.report 'strscan' do
TIMES.times do
scanner = StringScanner.new(s)
count = 0
count += 1 while scanner.scan_until(/yo/)
check count
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment