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:
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:
if that works, lets grab the first field off the line:
and also the query string:
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 as below, or enter http://melksh.am/nnnn for individual articlesQ804 - Object Orientation and General technical topics - Regular Expression Internals 
How do regular expressions work / Regular Expression diagrams - (2010-12-17) 
Matching to a string - what if it matches in many possible ways? - (2010-12-17) 
Macho matching - do not do it! - (2010-06-13) 
Next course - 7th January 2008, Regular Expressions - (2007-12-21)Q806 - Regular Expression Cookbook 
Getting more than a yes / no answer from a regular expression pattern match - (2012-06-30) 
Matching a license plate or product code - Regular Expressions - (2011-03-28) 
Regular Expression Myths - (2010-06-13) 
First and last match with Regular Expressions - (2010-04-02) 
Search and replace in Ruby - Ruby Regular Expressions - (2010-01-31) 
Efficient debugging of regular expressions - (2010-01-04) 
Making Regular Expressions easy to read and maintain - (2009-05-10) 
Validating Credit Card Numbers - (2008-10-14) 
Regular expressions made easy - building from components - (2007-08-16) 
Commenting a Perl Regular Expression - (2007-06-12) 
Keeping your regular expressions simple - (2006-04-05)U108 - Lua - Pattern matching 
Changing what operators do on objects - a comparison across different programming languages - (2014-12-26) 
Binary / bitwise operations in Lua with the standard bit32 library - (2012-04-06) 
Lua Regular Expressions - (2009-08-28) 
Lua - IAQ (Infrequently Answered Questions) - (2008-10-18) 
Lua examples, Lua Courses - (2008-08-08)
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