debugging and flow control
debugging and flow control
am 07.09.2007 13:33:05 von 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
Re: debugging and flow control
am 07.09.2007 16:46:17 von Paul Lalli
On Sep 7, 7:33 am, "alexxx.ma...@gmail.com"
wrote:
> 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
Re: debugging and flow control
am 08.09.2007 00:36:02 von Anno Siegel
On 2007-09-07 13:33:05 +0200, "alexxx.magni@gmail.com"
said:
> 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
Re: debugging and flow control
am 10.09.2007 09:54:04 von alexxx.magni
On 8 Set, 00:36, Anno Siegel wrote:
> On 2007-09-07 13:33:05 +0200, "alexxx.ma...@gmail.com"
> said:
>
> > 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
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
Re: debugging and flow control
am 10.09.2007 13:28:29 von Anno Siegel
On 2007-09-10 09:54:04 +0200, "alexxx.magni@gmail.com"
said:
> On 8 Set, 00:36, Anno Siegel wrote:
>> On 2007-09-07 13:33:05 +0200, "alexxx.ma...@gmail.com"
>> said:
>>
>>> 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.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 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
Re: debugging and flow control
am 10.09.2007 16:29:57 von Paul Lalli
On Sep 10, 7:28 am, Anno Siegel
wrote:
> On 2007-09-10 09:54:04 +0200, "alexxx.ma...@gmail.com"
> said:
>
> > 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.
Which is, of course, exactly what I did for the OP in the first reply
to this thread three days ago. . .
Paul Lalli