April 8, 2013, 6:09 pm - James Farrer
I was honestly a bit surprised at how long it took to process. I tried a few things to see what I could do to speed it up and was almost to the point of setting up a painful caching process when I decided to try my hand at manually writing the replacement script. Boy am I glad I did!
In the string the variables I am replacing are nicely bracketed in the format of "[[var_name]]". So I took the string and used the split function on it with "[[" as the delimiter. Then I took each row (minus the first that wouldn't have a variable in it) and split it again on the ending portion of "]]". Then I used a really simple associative array lookup with the first part of the second array and rebuilt the string with the results.
At first I couldn't believe the results, but after testing and verifying, I found that it was taking longer to log results than it was taking to actually do the processing. That's an improvement from 30,000+ ms down to about 10 ms.
Now I thought Regex might not be incredibly fast, but this truly amazed me. Not only is the syntax a bit cryptic, but it's slow. Next time I'll definitely think twice before using a regex.