|Syntactic Confectionery Delight|
Splitting a string to chunksby spurperl (Priest)
|on Nov 29, 2006 at 13:33 UTC||Need Help??|
spurperl has asked for the
wisdom of the Perl Monks concerning the following question:
Something that comes up fairly often is a need to split a string to equal sized chunks. For instance, given the string "abcdefgh12345678", splitting it to 4-char chunks would produce ("abcd", "efgh", "1234", "5678"). Looking around the monastery, there're at least a couple of posts I have found.
I tried to time some different techniques against each other:
And the results are quite surprising:
Contrary to what I have expected from my understanding (that built in functions should be faster than loops), the looping solution is the swiftest. It beats the unpack by a margin ranging from 15 to 50 percent, depending on the length of the string and the chunks.
Any way to make it faster ?