[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [Fun With Perl] How to swap $p and $q without $tmp



On Thu, Jun 10, 1999 at 10:16:45PM -0500, Tushar Samant wrote:
> 
>     $q^=$p^=$q^=$p
> 
> (probably well-known; if not, try it)

($p,$q) = ($q,$p);

$ perl -MBenchmark -e 'local $/;  open(STUFF, "/usr/dict/words") or die $!;  $data = <STUFF>;  $p = $data;  $q = reverse $data;  timethese(shift, { "bitwise" => sub { $q^=$p^=$q^=$p }, "list" => sub { ($p,$q)=($q,$p) }})'   
Benchmark: running bitwise, list, each for at least 3 CPU seconds...
   bitwise:  4 wallclock secs ( 3.22 usr +  0.00 sys =  3.22 CPU) @ 74.22/s (n=239)
      list:  3 wallclock secs ( 3.02 usr +  0.00 sys =  3.02 CPU) @ 127.48/s (n=385)

About 70% faster.

-- 

Michael G Schwern                                           schwern@pobox.com
                    http://www.pobox.com/~schwern
     /(?:(?:(1)[.-]?)?\(?(\d{3})\)?[.-]?)?(\d{3})[.-]?(\d{4})(x\d+)?/i

==== Want to unsubscribe from this list? (Don't you love us anymore?)
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org