No such thing as a small change | |
PerlMonks |
Re: Performance problems on splitting long stringsby Laurent_R (Canon) |
on Jan 31, 2014 at 18:33 UTC ( [id://1072892]=note: print w/replies, xml ) | Need Help?? |
The following is the post I prepared yesterday evening (after 2 a.m. this morning in fact), but I guess I was too tired: I previewed it, but stupidly forgot to hit the create button. OK, now i ran a benchmark with the various possibilities. The results might be useful to others. I tried 8 different solutions: a "C_style" solution (converting the string into an array of individual characters), two regexes (one with /\w{5}/ and one with /.{5}/, one with the opening of a file handler on a reference to the string, two variations on a loop with the substr function, the split solution offered by Kenosis (although I probably would not be able to use it with the old version of Perl that we have on our servers, but I could test it at home on my more recent version) and unpack. The following is the code (borrowed in part from Kenosis):
So unpack wins clearly the race, but I was surprised to see that substr is not that far behind. Update this evening (Jan 31, 2014 at 18:45): I incorporated the unpack solution in my program at work today, and the speed gain I obtained on my real data is significantly better than what could be derived from the figures of the benchmark above. The profiling shows that the modified code line runs surprisingly almost twice faster than the original one.
In Section
Seekers of Perl Wisdom
|
|