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