I am trying to write a simple script to parse
an Apache log file, but it is taking an extremely
long time. I used profile and the problem appears
to be with the regular expression matcher.
I have made a simple script to run it with different
lengths and it appears that the regular expression
matcher is being very slow. See the attached script.
Here are some timings:
~>time ./mklong.rb 1000
real 0m11.930s
~>time ./mklong.rb 2000
real 0m50.400s
~>time ./mklong.rb 3000
real 1m55.693s
~>time ./mklong.rb 4000
real 3m16.004s
So, dividing it out,
1000/11 = 91
2000/50 = 40
3000/120 = 25
4000/200 = 20
So, the matching appears to be much slower than O(n).
Isn’t the whole point of regular expressions to be
fast and O(n)?
Whenever my script encounters a long string,
it grinds to a halt.
Why is this?
Did I make the regular expression correctly?
Is there some way to optimize it?
Is there a problem with the matcher?
Any help would be appreciated.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2-ecc0.1.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
matcher is being very slow. See the attached script.
Whenever my script encounters a long string,
it grinds to a halt.
Why is this?
Did I make the regular expression correctly?
Is there some way to optimize it?
Is there a problem with the matcher?
Any help would be appreciated.
I’m not a regex expert, so I can’t help you with optimizing yours, but
if your pattern causes the regex engine to backtrack repeatedly, then it
will not match in linear time. This usually becomes apparent on long
strings where there can be lots of backtracking.
As you can see, I’ve simplified a few other things, such as including
anything that’s constant, minimising all the places where you’re
matching whitespace to simple spaces. And I added the anchor at the
beginning to match the start of the line, which is very important.
On Tue, Jul 25, 2006 at 03:59:32AM +0900, S3 wrote:
So, the matching appears to be much slower than O(n).
Isn’t the whole point of regular expressions to be
fast and O(n)?
No. The point behind regular expressions is to provide a succinct
mini-language for matching text that can be expressed using a Type-3
grammar[1] without the pain of having to write the code to implement the
necessary FSM. Speed is a secondary consideration, and down to the
person implementing the regex engine and the person writing the regex.
Especially the person writing the regex.
Whenever my script encounters a long string,
it grinds to a halt.
Why is this?
You’re making the matcher work harder than it needs to by not giving it
enough information about what it has to match. The bigger the file is,
the more it has to backtrack to match each line. See my note on anchors
below.
The regex engine will do as much as it can match the
Did I make the regular expression correctly?
Is there some way to optimize it?
Is there a problem with the matcher?
No, the matcher is fine.
There’s a few problems I can see straight off:
No anchors: without them, your regex ends up doing an awful lot more
work because it will end up attempting to match the pattern in the
middle of lines rather than only matching from the start of lines.
By putting ‘^’ at the start of your regex to match the beginning of
the line, you’ll see a big increase in performance. That’s probably
your single biggest problem.
It’s also possibly worth matching the end of the line with ‘$’.
To quote Jamie Zawinski:
"Some people, when confronted with a problem, think I know, I'll
use regular expressions. Now they have two problems."
If you don’t need a regex, don’t use them. In this case, try
reading the file in line by line (or slurp it all into memory if
it’s not too big), and break the line up along spaces using the
split() method.
Get Jeffrey Freidl’s Mastering Regular Expression[2]. You’ll thank
me
later.
Yow! Calling String#match is 2-3 times as slow! Why is that? The
overhead of calling the method?
I was curious about this too.
It looks like it’s the effort of computing a MatchData that takes all
the extra time. This snippet is just as slow as using #match:
bm.report ‘=~ literal + $~’ do
N.times do string =~ /a (.) c\s. e/ ; $~ end
end