Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
If you're concerned that the clearer version should be slower, you should benchmark it:
#!/usr/bin/perl -w use strict; use Benchmark; sub clarity { my $tmp = shift; my $ret = 0; if($tmp eq 'F'){ ; # do nothing, spot held for clarity }elsif($tmp =~ /[MICL]/){ $ret = 1; }else{ $ret = 2; } return $ret; } sub speed { my $tmp = shift; my $ret = 0; if($tmp =~ /[MICL]/){ $ret = 1; }else{ $ret = 2; } return $ret; } timethese(100_000, { clarity => sub { clarity('F'); clarity('M'); clarity('J'); }, speed => sub { speed('F'); speed('M'); speed('J'); }, } );
Benchmark: timing 100000 iterations of clarity, speed... clarity: 2 wallclock secs ( 1.74 usr + 0.00 sys = 1.74 CPU) @ 57 +471.26/s (n=100000) speed: 2 wallclock secs ( 1.78 usr + 0.00 sys = 1.78 CPU) @ 56 +179.78/s (n=100000)

So it appears that, at least in this test, the clearer version is a little faster. Your tests can simulate your environment better, but it looks like both chunks of code are really close to the same speed, and you should code however you find most clear and enjoyable. :)

Update: Abigail-II's right, this benchmark is a little off, and in fact a first attempt to correct it makes speed slightly faster, though it's still within a few percent. Still, you can easily see how to modify this to benchmark your actual functions and see if there is enough of a speed difference to sacrifice clarity.


In reply to Re: Style or Clarity? by sgifford
in thread Style or Clarity? by periapt

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (6)
    As of 2020-10-24 07:14 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My favourite web site is:












      Results (242 votes). Check out past polls.

      Notices?