Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

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

Let's look at the numbers:

#!/usr/local/bin/perl use Benchmark; use constant DEBUG_C => (1); sub DEBUG_S() { 1 } $DEBUG_V = 1; timethese( 4000000, { 'constant' => sub { my $x = 1 if DEBUG_C; my $y=2; }, 'subroutine' => sub { my $x = 1 if DEBUG_S; my $y=2; }, 'variable' => sub { my $x = 1 if $DEBUG_V; my $y=2; }, } );
produces the following (for DEBUG set to 1 and 0)
DEBUG 0, Run 1
Benchmark: timing 4000000 iterations of constant, subroutine, variable...
  constant:  3 wallclock secs ( 2.00 usr +  0.01 sys =  2.01 CPU) @ 1990049.75/s (n=4000000)
subroutine:  3 wallclock secs ( 1.99 usr +  0.00 sys =  1.99 CPU) @ 2010050.25/s (n=4000000)
  variable:  5 wallclock secs ( 3.93 usr +  0.00 sys =  3.93 CPU) @ 1017811.70/s (n=4000000)

DEBUG 0, Run 2
Benchmark: timing 4000000 iterations of constant, subroutine, variable...
  constant:  1 wallclock secs ( 1.99 usr +  0.00 sys =  1.99 CPU) @ 2010050.25/s (n=4000000)
subroutine:  1 wallclock secs ( 2.13 usr +  0.00 sys =  2.13 CPU) @ 1877934.27/s (n=4000000)
  variable:  3 wallclock secs ( 3.85 usr +  0.00 sys =  3.85 CPU) @ 1038961.04/s (n=4000000)

DEBUG 0, Run 3
Benchmark: timing 4000000 iterations of constant, subroutine, variable...
  constant:  1 wallclock secs ( 2.07 usr +  0.00 sys =  2.07 CPU) @ 1932367.15/s (n=4000000)
subroutine:  1 wallclock secs ( 2.19 usr +  0.00 sys =  2.19 CPU) @ 1826484.02/s (n=4000000)
  variable:  3 wallclock secs ( 3.78 usr +  0.00 sys =  3.78 CPU) @ 1058201.06/s (n=4000000)

DEBUG 1, Run 1
Benchmark: timing 4000000 iterations of constant, subroutine, variable...
  constant:  8 wallclock secs ( 7.47 usr +  0.00 sys =  7.47 CPU) @ 535475.23/s (n=4000000)
subroutine:  8 wallclock secs ( 7.49 usr +  0.00 sys =  7.49 CPU) @ 534045.39/s (n=4000000)
  variable:  9 wallclock secs ( 9.13 usr +  0.00 sys =  9.13 CPU) @ 438116.10/s (n=4000000)

DEBUG 1, Run 2
Benchmark: timing 4000000 iterations of constant, subroutine, variable...
  constant:  8 wallclock secs ( 7.53 usr +  0.00 sys =  7.53 CPU) @ 531208.50/s (n=4000000)
subroutine:  7 wallclock secs ( 7.41 usr +  0.00 sys =  7.41 CPU) @ 539811.07/s (n=4000000)
  variable:  8 wallclock secs ( 9.27 usr +  0.00 sys =  9.27 CPU) @ 431499.46/s (n=4000000)

DEBUG 2, Run 3
Benchmark: timing 4000000 iterations of constant, subroutine, variable...
  constant:  8 wallclock secs ( 7.38 usr +  0.00 sys =  7.38 CPU) @ 542005.42/s (n=4000000)
subroutine:  8 wallclock secs ( 7.23 usr +  0.00 sys =  7.23 CPU) @ 553250.35/s (n=4000000)
  variable:  9 wallclock secs ( 9.06 usr +  0.00 sys =  9.06 CPU) @ 441501.10/s (n=4000000)
Looking at those numbers - sure $DEBUG is a bit slower but I'd say the savings are a wash - especially if there's some network or database code also in the script. The amount of time you save from not using $DEBUG is really down in the noise. The key here is to choose one method and be consistent with it's application across all scripts/modules in your project(s).

-derby


In reply to Re: inlined DEBUG constant versus $DEBUG by derby
in thread inlined DEBUG constant versus $DEBUG by stefp

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 having a coffee break in the Monastery: (7)
As of 2024-04-16 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found