debugging and flow control

A

alexxx.magni

Hi all, I need some advice:

in the past, when writing programs I often used, to help me in
debugging, the following style:

my $debuglevel=1;
.....
print("var value is $a\n") if ($debuglevel>0);
.....
print(LOGFILE "array values are @x\n") if ($debuglevel>2);
....

you get the idea.
Now I'm working on a project very computational-intensive, and I'm
worried about the many lines "if ($debuglevel...)" to be evaluated.

I was wandering: if $debuglevel was a constant, not a variable, it is
possible that the program, when launched, evaluates those lines from
the beginning? In this case I should not worry about later evaluation.

Any other suggestion for flow control?

thanks!

Alessandro Magni
 
P

Paul Lalli

my $debuglevel=1;
....
print("var value is $a\n") if ($debuglevel>0);
....
print(LOGFILE "array values are @x\n") if ($debuglevel>2);
...

you get the idea.
Now I'm working on a project very computational-intensive, and I'm
worried about the many lines "if ($debuglevel...)" to be evaluated.

I was wandering: if $debuglevel was a constant, not a variable,
it is possible that the program, when launched, evaluates those
lines from the beginning? In this case I should not worry about
later evaluation.

That would appear to be accurate....

$ perl -MO=Deparse -e'
my $x = 1;
print "Hello " if $x;
print "World\n" if $x;
'
my $x = 1;
print 'Hello ' if $x;
print "World\n" if $x;
-e syntax OK


$ perl -MO=Deparse -e'
use constant x => 1;
print "Hello " if x;
print "World\n" if x;
'
use constant ('x', 1);
print 'Hello ';
print "World\n";
-e syntax OK


However, I'm not even a little bit convinced that the savings you'll
see from this will be noticeable. I'd try a Benchmark if I were you,
to see if it's worth your time to go through and change your code...

Paul Lalli
 
A

Anno Siegel

Hi all, I need some advice:

in the past, when writing programs I often used, to help me in
debugging, the following style:

my $debuglevel=1;
....
print("var value is $a\n") if ($debuglevel>0);
....
print(LOGFILE "array values are @x\n") if ($debuglevel>2);
...

you get the idea.
Now I'm working on a project very computational-intensive, and I'm
worried about the many lines "if ($debuglevel...)" to be evaluated.

Especially if the program is computational-intensive, it is unlikely
that skipping debug statements is going to make a noticeable difference.
I was wandering: if $debuglevel was a constant, not a variable, it is
possible that the program, when launched, evaluates those lines from
the beginning? In this case I should not worry about later evaluation.

If needed, that can be done using the constant pragma. The technique
is described in "perldoc constant", so I don't have to repeat it here.

Anno
 
A

alexxx.magni

Especially if the program is computational-intensive, it is unlikely
that skipping debug statements is going to make a noticeable difference.


If needed, that can be done using the constant pragma. The technique
is described in "perldoc constant", so I don't have to repeat it here.

Anno


Maybe I could be clearer: yes, I was talking about "use constant...".
Do you know if this:

use constant DEBUGON=>1;
if (DEBUGON>0) {...}

is evaluated, the if eliminated and the block {...} accepted, prior to
the global run?


thanks!

Alessandro
 
A

Anno Siegel

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Maybe I could be clearer: yes, I was talking about "use constant...".
Do you know if this:

use constant DEBUGON=>1;
if (DEBUGON>0) {...}

is evaluated, the if eliminated and the block {...} accepted, prior to
the global run?

There are at least two ways you could find out yourself. Take a look at
"perldoc constant" as recommended in my last reply. Or use the O::Deparse
function to see directly what the compiler makes of your statement.

Anno
 
P

Paul Lalli

There are at least two ways you could find out yourself. Take a
look at "perldoc constant" as recommended in my last reply. Or
use the O::Deparse function to see directly what the compiler
makes of your statement.

Which is, of course, exactly what I did for the OP in the first reply
to this thread three days ago. . .

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top