Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

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

I can think of two reasons to not use die/eval for (normal) control flow:

(1) Clarity. An exception is supposed to indicate — well, an exceptional condition: either an error, or a resource failure. Throwing an exception as a part of normal execution is liable to mislead maintainers of the code when they try to understand what’s going on. So, at the very least, it’s poor style.

(2) Efficiency. Consider the following code:

#! perl use strict; use warnings; use Benchmark qw(cmpthese); cmpthese(1_000_000, { 'bare_loop' => \&bare_loop, 'eval_loop' => \&eval_loop, }); sub bare_loop { my ($count, $evens, $flag) = (100, 0, 0); while ($count--) { next if $count % 2; # odd if ($count == 50) { $flag = 1; last; } ++$evens; } print "Normal loop exit\n" unless $flag; } sub eval_loop { my ($count, $evens) = (100, 0); eval { while ($count--) { next if $count % 2; # odd die if $count == 50; ++$evens; } }; print "Normal loop exit\n" unless $@; }

Typical output (on my machine):

Rate eval_loop bare_loop eval_loop 71541/s -- -23% bare_loop 92362/s 29% --

So, there is a definite performance penalty for throwing and then catching the exception. Not nearly as great a penalty as in a language like C++, but still — why opt for a less efficient method when the more efficient methods are just as easy to use?

Athanasius <°(((><contra mundum


In reply to Re^2: while loop question by Athanasius
in thread while loop question by Freezer

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 goofing around in the Monastery: (5)
As of 2024-04-18 23:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found