sábado, 10 de octubre de 2009

Benchmarking perl

I had a little code that worked perfectly for what it had to do (process every line in a file, and get a list of unique lines (after being processed)).

The 'process' I had to run on every line was a simple substitution, so I thought of Algorithm::Loops. I used it and worked really well, but then I thought about performance, and tried some other approaches:


Well I could clearly see that while version is the winner, and it really makes a big difference. I suppose this is because Filter takes <$fh> as an array (in list context), and that is slower than the iterator version (just guessing).

Here's the code:


And the results:

Rate filterChomp match regexBarraN while
filterChomp 9.49/s -- -19% -24% -64%
match 11.8/s 24% -- -6% -55%
regexBarraN 12.5/s 32% 6% -- -52%
while 26.0/s 174% 121% 108% --


Well, appart from keeping the while version for my app, now I have a template for bencharking perl codes. Following the same technique than in one of my other perl posts, I've written a template file to be easy to paste in a .pl file from vim.