in reply to Zero Padding

And if you dont want to use printf()/sprintf() because it slows your script down just work it out by yourself:
#!/usr/bin/perl $a=13; $length=6; while (length($a)<$length) {$a="0$a";} print $a;

-- tune

Replies are listed 'Best First'.
Re: Re: Zero Padding
by runrig (Abbot) on Feb 03, 2001 at 03:44 UTC
    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.

      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;
      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*

      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.

Re: Re: Zero Padding
by Martin A (Beadle) on Feb 03, 2001 at 19:38 UTC
    tune: while (length($a)<$length) {$a="0$a";}

    Humm.. why use a while loop. Wouldn't this be faster?

    $number = '0' x (5 - length($number)) . $number;

    // Martin