Regular Expression Efficiency
Posted by enquirer (enquirer), 30 September 2002A regex I wrote to parse our SunServer logfiles. From a quick glance...is it terribly inefficient?
Posted by admin (Graham Ellis), 30 September 2002Looks good and it's suprisingly efficient because it starts off with an anchor and there's a lot of very specific matches that mean there won't be a lot of going forward and backtracking. All your counts are specific or sparse - some authorities will tell you that sparse is slow; per character it IS but because it saves so much forward and back stuff, overall it'll make for a quicker match.
1. your match will fail if you use it on a data file from a server based in the USA, as they have a - not a + in the time zone difference field.
2. You don't need to specify $_ =~ - that's automatically there
3. If you save the result of your match into a list (such as
($ip,$date,$method ... etc ... ) = (m| .... etc ...);
the you have named variables for each part of the match which you might find easier to use and maintain laters that $11 and things like that!
Just tiny things - hey, it's a gooden!!!
Posted by enquirer (enquirer), 30 September 2002Would the 'o' modifier be good here? so that it compiles once?
Posted by admin (Graham Ellis), 30 September 2002The o modifier only effects regular expressions that include a variable witrhin the regular expression. Without a variable in the regular expression, Perl already knows that the expression won't change during the life of the program, so it only complies the regular expression once anyway.
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: firstname.lastname@example.org • WEB: http://www.wellho.net • SKYPE: wellho