logo       

Re: Slow regular-expression engine: msg#02391

ruby-talk

Subject: Re: Slow regular-expression engine

w_a_x_man <w_a_x_man@xxxxxxxxx> writes:

> This Awk code takes less than one hundredth of a second to run:
>
> BEGIN {
>
> regex = \
> "o?o?o?o?o?o?o?o?o?o?o?o?o?o?" \
> "o?o?o?o?o?o?o?o?o?o?o?o?o?" \
> "ooooooooooooooooooooooooooooo"
>
> print "ooooooooooooooooooooooooooooo" ~ regex
>
> }
>
>
> This Ruby code takes 27.5 seconds:
>
> t = Time.now
>
> regex = Regexp.new(
> "o?o?o?o?o?o?o?o?o?o?o?o?o?o?" +
> "o?o?o?o?o?o?o?o?o?o?o?o?o?" +
> "ooooooooooooooooooooooooooooo" )
>
> p "ooooooooooooooooooooooooooooo" =~ regex
>
> puts "#{ Time.now - t } seconds"
>
>
> See http://swtch.com/~rsc/regexp/regexp1.html

In Ruby 1.9 its 1/4 of the time (still slow).

But: One might ask if that is a problem at all, considering that this
Regexp looks and is awful and could be written in a way better way which
takes times of 0.000139965 seconds.

t = Time.now
regex = Regexp.new(/o{0,27}ooooooooooooooooooooooooooooo/ )
p "ooooooooooooooooooooooooooooo" =~ regex
puts "#{ Time.now - t } seconds"

--
Dominik Honnef
dominikho@xxxxxxx

Google Custom Search

News | Mail Home | sitemap | FAQ | advertise