On Thu, Feb 07, 2002 at 09:57:48PM +0800, [EMAIL PROTECTED] wrote:
>    From: "Sherwin Daganato" <[EMAIL PROTECTED]>
>    > 'exponentiation' won. It's 52% faster than 'bit_shift' @ 500000
>    > iterations.
>    i dont think so :->
>    [fooler@ns1 temp]$vi x.pl
>    #!/usr/bin/perl
>    $masklen = 24;
>    for ( $i = 1000000; $i; $i--) {
>            $mask = (2 ** ($masklen + 1)) - 1;
>    }
>    [fooler@ns1 temp]$time ./x.pl
>    real    0m9.847s
>    user    0m9.798s
>    sys     0m0.018s
>    [fooler@ns1 temp]$time ./x.pl
>    real    0m9.831s
>    user    0m9.821s
>    sys     0m0.002s
>    [fooler@ns1 temp]$vi x.pl
>    #!/usr/bin/perl
>    $masklen = 24;
>    for ( $i = 1000000; $i; $i--) {
>            $mask = (1 << ($masklen + 1)) - 1;
>    }
>    [fooler@ns1 temp]$time ./x.pl
>    real    0m7.372s
>    user    0m7.340s
>    sys     0m0.010s
>    [fooler@ns1 temp]$time ./x.pl
>    real    0m7.351s
>    user    0m7.346s
>    sys     0m0.002s

I don't know. I can't explain it.
I just couldn't believe the way you benchmark Perl codes.
If that's the right way then why would anyone create the
Benchmark module (see CPAN :-).

This link might help explain things:
http://www.foo.be/docs/tpj/issues/vol3_3/tpj0303-0009.html

I'm raising the iteration to get >10s of cpu time.

root@emc
/tmp (#1093) cat testbench.pl
#!/usr/bin/perl -w
use strict;
use Benchmark;
my $masklen = 24;
my $r=Benchmark::cmpthese(10000000,
{
  'bit_shift' => sub {
    my $mask =  (1 << ($masklen + 1)) - 1;
  },
  'exponentiation' => sub {
    my $mask = (2 ** ($masklen + 1)) - 1;
  }
});
exit;

root@emc
/tmp (#1094) w
 11:07pm  up 125 days, 14:48,  1 user,  load average: 0.00, 0.10, 0.09
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU  WHAT
root     pts/2    saturn.emc.com.p 10:12pm  0.00s  0.21s  0.01s  w

root@emc
/tmp (#1095) ./testbench.pl
Benchmark: timing 10000000 iterations of bit_shift, exponentiation...
 bit_shift: 15 wallclock secs (13.33 usr +  0.02 sys = 13.35 CPU) @
749063.67/s (n=10000000)
exponentiation: 12 wallclock secs (10.58 usr + -0.01 sys = 10.57 CPU) @
946073.79/s (n=10000000)
                   Rate      bit_shift exponentiation
bit_shift      749064/s             --           -21%
exponentiation 946074/s            26%             -- # << STILL WON

root@emc
/tmp (#1098) cat bit_shift.pl exponentiation.pl
#!/usr/bin/perl
   $masklen = 24;
   for ( $i = 1000000; $i; $i--) {
           $mask = (1 << ($masklen + 1)) - 1;
   }
#!/usr/bin/perl
   $masklen = 24;
   for ( $i = 1000000; $i; $i--) {
           $mask = (2 ** ($masklen + 1)) - 1;
   }

root@emc
/tmp (#1099) w
 11:19pm  up 125 days, 15:00,  1 user,  load average: 0.00, 0.03, 0.07
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU  WHAT
root     pts/2    saturn.emc.com.p 10:12pm  0.00s  0.24s  0.02s  w

root@emc
/tmp (#1100) time ./bit_shift.pl
1.67user 0.00system 0:01.67elapsed 99%CPU (0avgtext+0avgdata
0maxresident)k
0inputs+0outputs (260major+32minor)pagefaults 0swaps

root@emc
/tmp (#1107) w
 11:22pm  up 125 days, 15:03,  1 user,  load average: 0.00, 0.02, 0.06
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU  WHAT
root     pts/2    saturn.emc.com.p 10:12pm  0.00s  0.27s  0.01s  w

root@emc
/tmp (#1108) time ./exponentiation.pl
1.61user 0.00system 0:01.60elapsed 100%CPU (0avgtext+0avgdata
0maxresident)k
0inputs+0outputs (260major+32minor)pagefaults 0swaps



-- 
$_=q:; # SHERWIN #
70;72;69;6e;74;20;
27;4a;75;73;74;20;
61;6e;6f;74;68;65;
72;20;50;65;72;6c;
20;6e;6f;76;69;63;
65;27;:;;s=~?(..);
?=pack q$C$,hex$1;
;;;=ge;;;;;eval;;;
_
Philippine Linux Users Group. Web site and archives at http://plug.linux.org.ph
To leave: send "unsubscribe" in the body to [EMAIL PROTECTED]

To subscribe to the Linux Newbies' List: send "subscribe" in the body to 
[EMAIL PROTECTED]

Reply via email to