substr "000$num", -3 [download]
use strict; use Benchmark qw(cmpthese); my $num = 42; cmpthese (-3, { sprintf => sub { sprintf '%03d', $num }, concat => sub { ('0' x (3 - length $num)).$num }, substr => sub { substr "000$num", -3 }, }); Rate concat sprintf substr concat 244047/s -- -10% -36% sprintf 269956/s 11% -- -29% substr 381628/s 56% 41% -- [download]
MeowChow s aamecha.s a..a\u$&owag.print
In reply to (MeowChow) Re5: Help with number conversion by MeowChow in thread Help with number conversion by Mr.T