> > for ( $i = "001"; $i lt "501"; $i++ )
> 
> I think there's something to be said about using numbers that are really
> strings as numbers. Specifically that it might not be the RightThing to do.
> There seems to be an aweful lot of unnecessary conversion going on.

Here, Here.

> printf()'s formatting was designed for this task in mind:
> 
>  printf "%03d\n", $_ for ( 1 .. 501 );
> ---
> use Benchmark;
> 
> timethese( 10_000   , {
>      'NumsAsStrings' => 'for ( $i = "001"; $i lt "501"; $i++ ) {}',
>      'NumsAsNums1'   => 'for ( 1 .. 501 ) {};',
>      'NumsAsNums2'   => 'for ( $i=1; $i<501;$i++) {}'
>     });
> 
> __DATA__
> C:\temp>perl foo.pl
> Benchmark: timing 10000 iterations of NumsAsNums1, NumsAsNums2,
> NumsAsStrings...
> 
> NumsAsNums1:  2 wallclock secs ( 2.36 usr +  0.00 sys =  2.36 CPU)
> NumsAsNums2:  4 wallclock secs ( 3.51 usr +  0.00 sys =  3.51 CPU)
> NumsAsStrings:  4 wallclock secs ( 4.18 usr +  0.00 sys =  4.18 CPU)
> 
> C:\temp>perl foo.pl
> Benchmark: timing 10000 iterations of NumsAsNums1, NumsAsNums2,
> NumsAsStrings...
> 
> NumsAsNums1:  2 wallclock secs ( 2.24 usr +  0.00 sys =  2.24 CPU)
> NumsAsNums2:  4 wallclock secs ( 3.40 usr +  0.00 sys =  3.40 CPU)
> NumsAsStrings:  4 wallclock secs ( 4.17 usr +  0.00 sys =  4.17 CPU)
> ---
> 
> I guess now its a question of is printf() two times slower than print()?
> 

It appears so, maybe three.

> My two cents...
> 
> - Ron
> 

My nickel's worth (adjusting for inflation)

use Benchmark;

close STDERR;
open (STDERR, ">NUL");

timethese( 5000   , {
     'NumsAsStrings' => 'for ( $i = "001"; $i lt "501"; $i++ ) {print STDERR $i}',
     'NumsAsNums1'   => 'for ( 1 .. 501 ) {printf STDERR "%03d", $_};',
     'NumsAsNums2'   => 'for ( $i=1; $i<501;$i++) {printf STDERR "%03d", $i}'
    });

__END__

Benchmark: timing 5000 iterations of NumsAsNums1, NumsAsNums2, NumsAsStrings...
NumsAsNums1: 16 wallclock secs (15.33 usr +  0.02 sys = 15.35 CPU) @ 325.69/s (n=5000)
NumsAsNums2: 19 wallclock secs (19.19 usr +  0.00 sys = 19.19 CPU) @ 260.59/s (n=5000)
NumsAsStrings:  5 wallclock secs ( 5.63 usr +  0.02 sys =  5.65 CPU) @ 885.27/s 
(n=5000)

_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users

Reply via email to