On Thu, 7 Feb 2002, Sherwin Daganato wrote:
> 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 :-).
>
My mistake. I think which one is faster is system dependent. I run two
tests using my benchmarking code (bm.c):
1) Linux box P133:
mean execution time of exp.pl = 95749 usec
mean execution time of shift.pl = 96842 usec
exponentiation wins
2) Sun E450 4cpu:
mean execution time of exp.pl = 45268 usec
mean execution time of shift.pl = 44128 usec
shift wins
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
shimomura:~> more exp.pl
#!/usr/bin/perl
$masklen = 24;
for ( $i = 1000; $i; $i--) {
$mask = (2 ** ($masklen + 1)) - 1;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
shimomura:~> more shift.pl
#!/usr/bin/perl
$masklen = 24;
for ( $i = 1000; $i; $i--) {
$mask = (1 << ($masklen + 1)) - 1;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//this is bm.c, compile it with "gcc bm.c -o bm"
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#define ITERATIONS 100
long getTimeDiff(struct timeval t1, struct timeval t2);
int main (void)
{
struct timeval t1,t2;
static long exp_etime,shift_etime;
register int i;
for (i=0; i<ITERATIONS; i++){
gettimeofday(&t1,NULL);
system("/home/rowel/exp.pl");
gettimeofday(&t2,NULL);
exp_etime+=getTimeDiff(t1,t2);
gettimeofday(&t1,NULL);
system("/home/rowel/shift.pl");
gettimeofday(&t2,NULL);
shift_etime+=getTimeDiff(t1,t2);
}
printf("mean execution time of exp.pl = %ld usec\n", (exp_etime/=ITERATIONS));
printf("mean execution time of shift.pl = %ld usec\n",(shift_etime/=ITERATIONS));
if( exp_etime < shift_etime) printf("exponentiation wins\n");
else printf("shift wins\n");
return 0;
}
long getTimeDiff(struct timeval t1, struct timeval t2)
{
return (long) ((t2.tv_sec*1e6 + t2.tv_usec) - (t1.tv_sec*1e6 + t1.tv_usec));
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx
-rowel
_
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]