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

Ah! But if it is out and out speeed we are after then I might have offered :)

sub buk2 { $_[0] % 4 ? 0 : $_[0] % 100 ? 1 : not $_[0] % 400; } Rate browseruk isleap abigail buk2 browseruk 82.8/s -- -40% -54% -67% isleap 138/s 67% -- -24% -45% abigail 180/s 118% 31% -- -28% buk2 251/s 203% 82% 39% --

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


Replies are listed 'Best First'.
Re3: Anyone use "xor" in conditionals?
by dragonchild (Archbishop) on Jul 15, 2003 at 02:17 UTC
    And why haven't you offered this as a patch to Date::Leapyear or any of the other Date:: modules? Hmmm?!? Or, even better, offer both the pureperl and XS versions ...

    In other words, it's through these incremental improvements that modules are made as strong as they can be. That kind of code is exactly the code that belongs in modules. They're supposed to fast, commented, and full of black magic. That's why modules exist!

    ------
    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.

      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


        ++ 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.

Re: Anyone use "xor" in conditionals?
by Abigail-II (Bishop) on Jul 15, 2003 at 07:05 UTC
    I wasn't looking for the fastest solution to this particular problem. My 'solution' looked similar to the one from the Date module on purpose. I wanted to show the effect of changing the order, so I used the same style which would make it more obvious what the effect is.

    Nested ?: is of course faster than repeated statement modifiers.

    Abigail

    PS: Could you fix your sig? It doesn't seem to close its font element - replying in a textform with fontsize = 1 is very annoying.

      Hopefully, my sig. is now fixed.


      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