in reply to Re3: Anyone use "xor" in conditionals?
in thread Anyone use "xor" in conditionals?

Mostly cos I'd never even considered looking at, never mind using a module to determine whether a year was a leapyear or not.

I only looked up Date::Leapyear in order to find a representative example of "the conventional way". I was only responding to the question about xor, and never gave a thought to the performance until Abigail-II (rightly) posted the benchmark.

From there, I couldn't not rise to the challenge could I :).


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


  • Comment on Re: Re3: Anyone use "xor" in conditionals?

Replies are listed 'Best First'.
Re: Re: Re3: Anyone use "xor" in conditionals?
by zengargoyle (Deacon) on Jul 15, 2003 at 03:36 UTC

    ++ BrowserUK for thinking like i do. :) how about this little gem from Net::Netmask...

           sort_by_ip_address       This function is included in "Net::Netmask"
                                    simply because there doesn't seem to be a bet-
                                    ter place to put it on CPAN.  It turns out
                                    that there is one method for sorting dotted-
                                    quads ("a.b.c.d") that is faster than all the
                                    rest.  This is that way.  Use it as
                                    "sort_by_ip_address(@list_of_ips)".
    
    sub sort_by_ip_address { return sort { pack("C4",split(/\./,$a)) cmp pack("C4",split(/\./,$ +b)) } @_ }

    which has to be about the worst possible.

      which has to be about the worst possible.

      Prove it.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        use Benchmark qw(cmpthese); sub rbyte { int rand 256 } my @unsorted; for (1..10000) { push @unsorted, join '.', rbyte, rbyte, rbyte, rbyte +} sub sort_by_ip_address { return sort { pack("C4",split(/\./,$a)) cmp pack("C4",split(/\./,$b) +) } @_ } sub sort_faster { return map {$_->[1]} sort {$a->[0] cmp $b->[0]} map {[(pack('C4',spl +it(/\./,$_))), $_]} @_ } my @sorted; cmpthese( 10, { ' slow' => sub { @sorted = sort_by_ip_address(@unsorted) }, 'faster' => sub { @sorted = sort_faster(@unsorted) }, }); __END__ $ perl fastip.pl Benchmark: timing 10 iterations of slow, faster... slow: 80 wallclock secs (40.79 usr + 0.00 sys = 40.79 CPU) @ 0 +.25/s (n=10) faster: 15 wallclock secs ( 6.95 usr + 0.06 sys = 7.01 CPU) @ 1 +.43/s (n=10) s/iter slow faster slow 4.08 -- -83% faster 0.701 482% --