in reply to Re: Re: sprintf vs. substr (for zero-padded values)
in thread sprintf vs. substr (for zero-padded values)
You're correct in your assumption that substr is faster, so long as the only number you need to process is "213" and you bury it in a subroutine call. You'll have to admit that in the real world users tend to ask for a little more functionality than that.use Benchmark qw[cmpthese]; sub one { sprintf("%09d",@_); } sub two { substr("000000000$_[0]",-9); } cmpthese(100, { one => sub { for(0..10000) { $_ = one($_) } }, two => sub { for(0..10000) { $_ = two($_) } }, tre => sub { for(0..10000) { $_ = sprintf("%09d", $_) } }, }); __DATA__ Benchmark: timing 100 iterations of one, tre, two... one: 12 wallclock secs (10.63 usr + 0.00 sys = 10.63 CPU) @ 9 +.41/s (n=100) tre: 3 wallclock secs ( 3.38 usr + 0.00 sys = 3.38 CPU) @ 29 +.59/s (n=100) two: 12 wallclock secs (11.08 usr + 0.00 sys = 11.08 CPU) @ 9 +.03/s (n=100) Rate two one tre two 9.03/s -- -4% -69% one 9.41/s 4% -- -68% tre 29.6/s 228% 214% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re^3: sprintf vs. substr (for zero-padded values)
by Theseus (Pilgrim) on Jul 18, 2002 at 13:30 UTC | |
by Abigail-II (Bishop) on Jul 18, 2002 at 13:38 UTC |