debugging and flow control

Discussion in 'Perl Misc' started by alexxx.magni@gmail.com, Sep 7, 2007.

  1. Guest

    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
    , Sep 7, 2007
    #1
    1. Advertising

  2. Paul Lalli Guest

    On Sep 7, 7:33 am, "" <>
    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
    Paul Lalli, Sep 7, 2007
    #2
    1. Advertising

  3. Anno Siegel Guest

    On 2007-09-07 13:33:05 +0200, ""
    <> 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
    Anno Siegel, Sep 7, 2007
    #3
  4. Guest

    On 8 Set, 00:36, Anno Siegel <-berlin.de> wrote:
    > On 2007-09-07 13:33:05 +0200, ""
    > <> 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
    , Sep 10, 2007
    #4
  5. Anno Siegel Guest

    On 2007-09-10 09:54:04 +0200, ""
    <> said:

    > On 8 Set, 00:36, Anno Siegel <-berlin.de> wrote:
    >> On 2007-09-07 13:33:05 +0200, ""
    >> <> 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
    Anno Siegel, Sep 10, 2007
    #5
  6. Paul Lalli Guest

    On Sep 10, 7:28 am, Anno Siegel <-berlin.de>
    wrote:
    > On 2007-09-10 09:54:04 +0200, ""
    > <> 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
    Paul Lalli, Sep 10, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lad

    os.system and flow control

    Lad, Oct 14, 2004, in forum: Python
    Replies:
    3
    Views:
    363
    George Kinney
    Oct 15, 2004
  2. Coleen
    Replies:
    2
    Views:
    1,366
    Coleen
    Aug 31, 2006
  3. Shawn
    Replies:
    3
    Views:
    328
    Oliver Wong
    Nov 29, 2006
  4. Steven T. Hatton

    Types as tags and RTTI for flow control

    Steven T. Hatton, Mar 2, 2006, in forum: C++
    Replies:
    3
    Views:
    301
    Steven T. Hatton
    Mar 6, 2006
  5. Jack Dowson
    Replies:
    0
    Views:
    453
    Jack Dowson
    May 7, 2007
Loading...

Share This Page