in reply to Testing a number for oddness
So I did it this way:
sub perl_mod { for my $z (1..10_000) { $_[0] % 2; } } sub perl_and { for my $z (1..10_000) { $_[0] & 1; } } timethese(10_000, { perl_mod => sub { perl_mod($num) }, perl_and => sub { perl_and($num) }, });
and got the following results:
nicholas(/5)@neko [106 /<1>nicholas/tmp] > ./time Benchmark: timing 10000 iterations of perl_and, perl_mod... perl_and: 210 wallclock secs (209.32 usr + 0.79 sys = 210.11 CPU) perl_mod: 260 wallclock secs (258.62 usr + 0.93 sys = 259.55 CPU) nicholas(/5)@neko [107 /<1>nicholas/tmp] >
Which shows the perl_and to be around 20% faster than perl_mod: about what I would expect.
Then, I did a pure C test:
and ran it seperately for each of the types (I was using /bin/time to test):#include <stdio.h> void c_and(int i) { int a; for (a=0;a<10000;a++) { i & 1; } } void c_mod(int i) { int a; for (a=0;a<10000;a++) { i % 2; } } int main(void) { int a; for (a=0;a<100000;a++) { c_and(a); } //for (a=0;a<100000;a++) { // c_mod(a); //} exit(0); }
andFor c_and: 29.800u 0.040s 0:29.84 100.0% 0+0k 0+0io 69pf+0w
for c_mod: 29.540u 0.390s 0:29.99 99.7% 0+0k 0+0io 69pf+0w
Which again shows and to be faster, though no wheres as much of a difference as with perl (I presume gcc is optimized).
test platform: dual PII-233 with a load avg of around 7 :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Testing a number for oddness
by Anonymous Monk on Jan 23, 2001 at 22:41 UTC | |
|
Re: Re: Testing a number for oddness
by runrig (Abbot) on Jan 24, 2001 at 05:58 UTC |