<banzaaaaiiiiii>

On Mon, Jul 01, 2002 at 10:54:26AM +0100, Mark Fowler wrote:
> So...I've got this tight loop where I need to whip a single character out
> of a ten char long string.  

The Perl Character Cleaving Conundrum

> Now this is Perl, so there's more than one way
> to do it, unpack and substr.  Which is faster?

Place bets now!

[tick...tick...tick]

Betting Ends!

> mark@pate:~/bench$ time perl unpack >/dev/null

If you say "unpack", you a loser!

> mark@pate:~/bench$ time perl substr >/dev/null

If you say "substr", you the winner!

</banzaaaaiiiiii>

> So substr is much faster.  Did this surprise anyone?  

No, not really.

unpack is generic, substr is specific (i.e. substr grabs characters from 
strings whereas unpack grabs variable-length entities from a collection
of bits and bytes).

More specialised nearly always means "faster and smaller".

A


Reply via email to