I made an extraordinary claim on Thursday. One of my delegates had written a program that analyzed a
huge data file in a Lua script, and it was taking over half an hour to run.
"Give me five minutes, and I could have that running in less that five minutes" I claimed.
It was all to do with pattern matching in Lua ... and I was pretty sure of myself, but my delegates were less sure of the claim. But it didn't really matter - it would have been a diversion from the main course if we had sidetracked to test my assertion.
But when I make a claim, and that claim is doubted, I worry. Was my claim reasonable, or wash it brash and overoptimistic? Well - how better to find out than to try it.
Try it, I did ... the original takes 3283 c.p.u. seconds to run - that's analyzing a 43 Mbyte log file looking for search engine arrivals, and extracting the search terms that were used.
In the original, the lookup was simple on each line:
string.find(line,'(%S+).*"http.*%.google%..*[&?]q=(.-)[&"]')
in other words, grab the first value on the line, then look for the referer, check if it's google, and if it is grab the query string.
It works - well - but s-l-o-w-l-y.
Let's try to speed that up. Rather than analyse every line starting with wild cared groups of indefinite size (which are a VERY fuzzy, so slow) match, let's start off by looking specifically for the google reference:
string.find(line,'%.google%.')
if that works, lets grab the first field off the line:
string.find(line,'(%S+)')
and also the query string:
string.find(line,'"http.-[&?]q=(.-)[&"]')
Now your natural reaction may be to say "that code is longer and more complicated" - and indeed it is - "so it will take longer", but it turns out THAT conclusion is utterly incorrect.
The original took 3283 seconds. The modified code took (bang the symbol, roll the drums) ... 3.25 seconds. That's right - my claim that I could reduce the time from 30 minutes to 5 minutes was dreadfully conservative - I actually got it down to less that 5 seconds, or put another way it now runs 1000 times faster.
Although this example is written with Lua's pattern matching as the examples, exactly the same principles apply to regular expressions in Perl, PHP, Python, Ruby, Tcl ... etc.
• Start matches with anchors and literals if possible
• Try to avoid too many long woolly matches such as .+
• Eliminate lines / cases quickly if you can
• Don't try to do it all in one long pattern - several shorter ones are often much faster.
Source code of slow version -
[here]
Source code of fast version -
[here]
(written 2010-04-16, updated 2010-05-14)
Associated topics are indexed under
U108 - Lua - Pattern matching [2702] First and last match with Regular Expressions - (2010-04-02)
[2383] Lua Regular Expressions - (2009-08-28)
[1847] Lua - IAQ (Infrequently Answered Questions) - (2008-10-18)
[1744] Lua examples, Lua Courses - (2008-08-08)
Q804 - Object Orientation and General technical topics - Regular Expression Internals [3091] How do regular expressions work / Regular Expression diagrams - (2010-12-17)
[3090] Matching to a string - what if it matches in many possible ways? - (2010-12-17)
[2806] Macho matching - do not do it! - (2010-06-13)
[1480] Next course - 7th January 2008, Regular Expressions - (2007-12-21)
Q806 - Regular Expression Cookbook [3218] Matching a license plate or product code - Regular Expressions - (2011-03-28)
[2804] Regular Expression Myths - (2010-06-13)
[2608] Search and replace in Ruby - Ruby Regular Expressions - (2010-01-31)
[2563] Efficient debugging of regular expressions - (2010-01-04)
[2165] Making Regular Expressions easy to read and maintain - (2009-05-10)
[1840] Validating Credit Card Numbers - (2008-10-14)
[1305] Regular expressions made easy - building from components - (2007-08-16)
[1230] Commenting a Perl Regular Expression - (2007-06-12)
[672] Keeping your regular expressions simple - (2006-04-05)
Some other Articles
Training Courses - time of changeBird Watching on the Melksham AvonUploading a document or image to its own URL via a browserRedirecting a home page using mod_rewriteMaking a Lua program run more than 10 times fasterEvery cloud has a silver liningHow do our tactics help us meet the strategy, for the greater good?Escapee Grandparents welcome - Henfield and MelkshamReturning multiple values from a function - LuaMixins example in Python