On Thu, Feb 07, 2002 at 02:29:51PM +0800, [EMAIL PROTECTED] wrote:
>
> On Thu, 7 Feb 2002, Andre John Cruz wrote:
> > i know this has already been way off topic :) but exponentiation with an
> > integer exponent and base 2 is easier done by having bit shifts <<
> >
> > -andre
> >
> but bit shift << consumes much less cpu cycles than
> exponentiation of 2 to an int.
> -rowel
There's only one way to find out. Let's do the Perl benchmarking. �:�p
Consider the following codes from Orly:
1. my $mask = (1 << ($masklen + 1)) - 1;
2. my $mask = (2 ** ($masklen + 1)) - 1;
Let no. 1 become 'bit_shift' and let no. 2 become 'exponentiation'.
Tada...
<benchmark>
Benchmark: timing 500000 iterations of bit_shift, exponentiation...
bit_shift: 1 wallclock secs ( 2.25 usr + 0.00 sys = 2.25 CPU) @
222222.22/s (n=500000)
exponentiation: 1 wallclock secs ( 1.48 usr + 0.00 sys = 1.48 CPU) @
337837.84/s (n=500000)
Rate bit_shift exponentiation
bit_shift 222222/s -- -34%
exponentiation 337838/s 52% --
</benchmark>
'exponentiation' won. It's 52% faster than 'bit_shift' @ 500000
iterations.
--
$_=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]