Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > performance stats of String#scan, strscan and a homemade approach

Reply
Thread Tools

performance stats of String#scan, strscan and a homemade approach

 
 
Simon Strandgaard
Guest
Posts: n/a
 
      08-03-2004
because I recently have messed around with a ruby syntax colorer,
I needed to know more about the performance of #scan or if there
were faster alternatives.. String#scan seems to be the fastest.

maybe this come others in handy.

--
Simon Strandgaard


bash-2.05b$ ruby h.rb
user system total real
String#scan 0.810000 0.020000 0.830000 ( 0.937981)
strscan 1.110000 0.040000 1.150000 ( 1.255724)
homemade slicer 2.420000 0.130000 2.550000 ( 2.648530)
true
true
bash-2.05b$ expand -t2 h.rb
require 'strscan'
def strscan(string, re)
tokens = []
ss = StringScanner.new(string)
until ss.eos?
m = ss.scan(re)
break unless m
tokens << m
end
tokens
end
def slicer(string, re)
tokens = []
while string.size > 0
m = re.match(string)
break unless m
token = string.slice!(0, m.end(0))
tokens << token
end
tokens
end
re_src = '\d+|\s+|.'
n = 10000
require 'benchmark'
Benchmark.bm(20) do |b|
# Exercise String#scan
re1 = Regexp.new(re_src)
lines = IO.readlines(__FILE__)
result1 = []
GC.disable
b.report("String#scan") do
n.times do |i|
result1 << lines[i%lines.size].scan(re1)
end
end
GC.enable
# Exercise strscan
lines = IO.readlines(__FILE__)
result2 = []
GC.disable
b.report("strscan") do
n.times do |i|
result2 << strscan(lines[i%lines.size], re1)
end
end
GC.enable
# Exercise homemade slicer
re2 = Regexp.new('\A(?:'+re_src+')')
lines = IO.readlines(__FILE__)
result3 = []
GC.disable
b.report("homemade slicer") do
n.times do |i|
result3 << slicer(lines[i%lines.size].clone, re2)
end
end
GC.enable
# check that output was correct
p((result1 == result2), (result1 == result3))
end
bash-2.05b$


 
Reply With Quote
 
 
 
 
Michael Neumann
Guest
Posts: n/a
 
      08-03-2004
Simon Strandgaard wrote:
> because I recently have messed around with a ruby syntax colorer,
> I needed to know more about the performance of #scan or if there
> were faster alternatives.. String#scan seems to be the fastest.
>
> maybe this come others in handy.
>
> --
> Simon Strandgaard
>
>
> bash-2.05b$ ruby h.rb
> user system total real
> String#scan 0.810000 0.020000 0.830000 ( 0.937981)
> strscan 1.110000 0.040000 1.150000 ( 1.255724)
> homemade slicer 2.420000 0.130000 2.550000 ( 2.648530)
> true
> true
> bash-2.05b$ expand -t2 h.rb
> require 'strscan'
> def strscan(string, re)
> tokens = []
> ss = StringScanner.new(string)
> until ss.eos?
> m = ss.scan(re)


There's no advantage in your case when using strscan. Try to store the
positions of the tokens and not the scanned string itself (the token) =>
StringScanner#skip. But that might not work for you.

Regards,

Michael


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Homemade STL - how to make "specific" constructor and istream>> operator Bubba C++ 1 04-15-2011 11:21 PM
Get Interface stats NOT VLAN stats on 6500 Mr Jibbles Cisco 3 07-03-2006 05:40 PM
Help copying file from homemade DVD - Data error message Arawak Computer Support 1 10-06-2004 01:27 PM
homemade pc case al Computer Support 6 11-17-2003 08:23 AM
Strscan ruby 1.8 why this doesn't work ? Domingo Alvarez Duarte Ruby 2 08-22-2003 07:54 AM



Advertisments