Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re^8: Given When Syntax by Laurent_R
in thread Given When Syntax by Deep_Plaid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-03-29 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found