Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^8: Given When Syntax

by Laurent_R (Canon)
on Mar 17, 2014 at 23:10 UTC ( #1078701=note: print w/replies, xml ) Need Help??

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

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.

No, switches on consecutive numbers are certainly less common, but not unlikely. The OP describes a situation where the switch is done on the first digit of a string always starting with a digit. The values can only be 0 to 9. I have seen many similar cases where decision have to be taken on the basis of two (or three) specific digits of a telephone number or an IMSI number, or the first byte of an IP address, or the length of words in a phrase, some sequentially ordered items, etc. This is far from uncommon, and there is no reason not to use an array when that occurs.

Arbitrary keys in dispatch tables need hashes.

Yes, and I said on an earlier post in this thread that dispatch tables are usually hashes whose values are coderefs. But if they are not arbitrary values but consecutive numbers, as in the case in point, why not think out of the box and use an array if that is likely to be more efficient (although the difference is only marginal.

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

Mines return undef, a perfectly admissible default case.

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

First, I am only very mildly interested about discussing about goto solutions. Second, this has nothing to do with the problem outlined in the OP. Third, the goto solution is not very efficient. Yet, granted, there may be cases where it might simplify the code.

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)

Yes, that is absolutely true, but off-topic. The given/when solution is clearly deprecated (well, "highly experimental" is the official expression) at this point.

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.

Not sure to understand what you mean, but, again, I only compared solutions as they were offered by their original posters, nothing more.

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

Methinks that I clearly stated that there was a penalty in using dispatch tables. I only said that the penalty was not as large as most people say, especially when the subs are really doing some work. The dispatch table option I used in my benchmark were able to process more than 1.5 million requests per second on my poor laptop.

I made some timings a few months ago on a program that had to split input data between about 8 or 9 output files, based on somewhat complex conditions on the input data. The input data was about 30 million lines and the execution time about 13 minutes. I tried two different approaches: a traditional C-style approach, and a heavily HOP oriented approach (with dispatch tables, function factories, etc.). I did not benchmark them properly, because it would have taken ages, but I timed them several times. The largest execution time difference that I found was, if I remember correctly, 16 seconds, and the average difference about 10 sec. Does that matter? I don't think so when we speak about more than 10 min execution time. One program was 300 lines, the other just 70. Guess which ones?

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

Thank you for the lesson, it looks like I did not know, sorry. As I said, I used various people solutions as they were written, including yours. Your goto solution was faster than your hash solution, probably because one used the substr function and the other a regex. These are YOUR solutions as you wrote them, please don't blame me on that.

Having said all that, I'll post soon a new benchmark in which I am trying to make things more comparable. It will shed a different light of this.

Replies are listed 'Best First'.
Re^9: Given When Syntax
by LanX (Sage) on Mar 17, 2014 at 23:19 UTC
    > ... I am only very mildly interested about discussing about goto solutions.

    Yeah, and I'm mildly interested to invest more time...

    Just continue benchmarking code you are only mildly interested to understand.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Alright, you don't want to discuss real substance, it is your choice. I am sorry that we got there, I did not want that. And contrary to you, I will not downvote your post for that disagreement.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1078701]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2022-09-27 11:45 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (119 votes). Check out past polls.