http://www.perlmonks.org?node_id=1078609


in reply to Re^3: Given When Syntax
in thread Given When Syntax

Hi Rolf, there may be some errors in the benchmark, but then you should say what is wrong, rather than just implying it was not done properly.

Replies are listed 'Best First'.
Re^5: Given When Syntax
by LanX (Saint) on Mar 17, 2014 at 15:22 UTC
    I'll post benchmarks after GPW.

    Sorry but ATM you are mangling different things and effectively comparing apples with plastic food.

    BTW as it seems you are also "proving" that gotos to labels with code execution are faster then simple hash look ups. (which would support using goto)

    update

    I'm not sure but I somehow remember benchmarks indicating that goto was not very efficiently implemented.

    By having a linear complexity, it could be in small cases faster than the constant overhead of hash lookups.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      BTW as it seems you are also "proving" that gotos to labels with code execution are faster then simple hash look ups.

      The difference might come from something else, such as the use of a regex on the one hand, and of substr on the other hand for extracting the first digit. I did not change them because I wanted to use the various solutions as posted, or as close as possible to the way they were posted. After all, the way to get the first digit is also part of the solution. But, of course, if one wants to compare only the process for finding the values associated with that digit, then the easiest is to remove the digit extraction part by passing a single integer to the function. That was not my goal when posting the benchmark above, but I can certainly do that later when I get home, but not right now.

        There is more to do, switches on consecutive numbers are very unlikely, so array solutions don't prove anything, even if the example is so simplistic.

        Arbitrary keys in dispatch tables need hashes.

        Furthermore all my solutions are able to define a "default" case, like given/when does.

        Then goto's allow jumping to multiple lables leading to the same code, for this you need multiple entries in a dispatch hash.

        And it should be clear (but mostly ignored/assumed in this thread) that given/when's linear testing of conditions is more flexible then switching to code by literal keys . (C-style I suppose)

        The latter is an important but limited sub-case, which is ideally optimized under the hood, but measuring both side by side is like comparing apples and oranges.

        > I did not change them because I wanted to use the various solutions as posted,

        yes but you made (rather bold) statements about the execution time of "sub-calls" by benchmarking these arbitrary snippets.

        edit

        Better you concentrate on this point and try variations on the number of cases.

        I hope its evident that "clean" benchmarks are not trival.

        Cheers Rolf

        ( addicted to the Perl Programming Language)