One can improve performance even further by using
constant.
$ cat 645310.pl
use strict;
use warnings;
use Benchmark qw/ cmpthese /;
our $verbosity = 7;
our $volume = 1;
use constant VERBOSE => 0;
cmpthese(
-1,
{
'Name7' => ' if ( $volume >= $verbosity ) { v_print3( "foo" ) }; '
+,
'Name8' => ' if ( VERBOSE ) { v_print3( "foo" ) }; '
+,
}
);
sub v_print3 { my $message = shift; print $message; }
__END__
$ perl 645310.pl
Rate Name7 Name8
Name7 9442579/s -- -74%
Name8 35826879/s 279% --
The whole subroutine call gets optimized away:
$ perl -MO=Deparse,-x,-p -e 'use constant VERBOSE => 0; if ( VERBOSE )
+ { v_print3( "foo" ) }; '
use constant ('VERBOSE', 0);
'???';
-e syntax OK
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.