in reply to Re: Zero Padding
in thread Zero Padding

A quick Benchmark will show that this repeated concatenation is slower than sprintf:
#!/usr/local/bin/perl use Benchmark; my $var=13; my $length=6; timethese(100000, { CONCAT=>\&concat, SPRINTF=>\&spr_f, }); sub concat { my $a = $var; while (length($a)<$length) {$a="0$a";} } sub spr_f { my $a = $var; sprintf("%0$length", $a); } Results: Benchmark: timing 100000 iterations of CONCAT, SPRINTF... CONCAT: 2 wallclock secs ( 1.53 usr + 0.00 sys = 1.53 CPU) @ 65 +359.48/s (n=100000) SPRINTF: 0 wallclock secs ( 0.60 usr + 0.00 sys = 0.60 CPU) @ 16 +6666.67/s (n=100000)
UpdateOops, fixed the bug, tye's right that it doesn't change the result much, but I didn't really want to return the correct value in concat() or make spr_f() extra fast, just wanted to compare the process to create the desired result.

Replies are listed 'Best First'.
(tye)Re: Zero Padding
by tye (Sage) on Feb 03, 2001 at 04:08 UTC

    You had a bug in spr_f() (you end up using a format of "%06" instead of "%06d"). That didn't seem to affect the speed much, though. You also don't return the correct value from concat() so I fixed that too. I made spr_f() a bit faster by eliminating the extra copy. This penalizes concat() since it is now the only one that needs the extra copy (oh well).

    I added my favorite (that I learned from chip) and reran the benchmark:

    #!/usr/bin/perl -w use strict; use Benchmark qw( cmpthese ); my $num= 13; my $wid= 6; sub concat { my $a= $num; $a= "0$a" while length($a) < $wid; $a; } sub spr_f { sprintf( "%0${wid}d", $num ); } sub sub_s { substr( "0"x$wid.$num, -$wid ); } cmpthese( -3, { CONCAT => \&concat, SPRINTF => \&spr_f, SUBSTR => \&sub_s, }); __END__ Your Results: Benchmark: timing 100000 iterations of CONCAT, SPRINTF... CONCAT: 65359.48/s SPRINTF: 166666.67/s Mine: Rate CONCAT SPRINTF SUBSTR CONCAT 98370/s -- -62% -66% SPRINTF 257009/s 161% -- -10% SUBSTR 286165/s 191% 11% --

    Looks like the substr() is fastest.

            - tye (but my friends call me "Tye")
      This edges out substr() in my benchmarks:

      my $padded = '0' x ($wid - length $num) . $num;

        And it doesn't have the perhaps significant disadvantage of my favorite substr method of truncating numbers that are too wide. ):

                - tye (but my friends call me "Tye")
Re (tilly) 3: Zero Padding
by tilly (Archbishop) on Feb 03, 2001 at 05:11 UTC
    True, but it should be obvious without pulling out Benchmark that the algorithm is quadratic. Just look at how much recopying of data there is.

    *shrug*

Re: Re: Re: Zero Padding
by Anonymous Monk on Feb 03, 2001 at 03:49 UTC
    try this
    $foo = 0000
    $foo .= $number
      In the spirit of TIMTOWTDI, I offer my version from my FPBASIC days:
      my $num=42; my $padto="000000"; # padding to 6digs $num=$padto.$num; print substr($num,-6), "\n";
      Not the shortest, but a valid solution. Of course, I'd use printf() as mentioned zillions of times.

      --
      Me spell chucker work grate. Need grandma chicken.