The other day, a friend asked me a seemingly simple question: what's the best way to convert a list of integers into a string, presuming that the integers are ASCII values. For instance, the list [97, 98, 99] should be converted to the string 'abc'. Let's assume we want to write a function to do this.
This exercise in optimization (in Python) is pretty fascinating (via fold).
Just for fun, I rewrote most of the functions in Perl (you can check my work in this gist); the results are actually somewhat interesting (
f1() is very fast in Perl, for instance, while it's one of the slower implementations in Python). The results, using a 256-item list:
f3 6950/s -- -21% -50% -74% -98% f6 8839/s 27% -- -37% -67% -98% f2 14023/s 102% 59% -- -48% -97% f1 26920/s 287% 205% 92% -- -94% f7 421642/s 5967% 4670% 2907% 1466% --
The fastest in both languages is probably the one you'd expect; in Perl, it's using
pack, which is hugely faster than anything else.