Optimizing constants in loops

Discussion in 'Python' started by Michael Hoffman, Jun 13, 2007.

  1. The peephole optimizer now takes things like

    if 0:
    do_stuff()

    and optimizes them away, and optimizes away the conditional in "if 1:".

    What if I had a function like this?

    def func(debug=False):
    for index in xrange(1000000):
    if debug:
    print index
    do_stuff(index)

    Could the "if debug" be optimized away on function invocation if debug
    is immutable and never reassigned in the function? When performance
    really matters in some inner loop, I usually move the conditional
    outside like this:

    def func(debug=False):
    if debug:
    for index in xrange(1000000):
    print index
    do_stuff(index)
    else:
    for index in xrange(1000000):
    do_stuff(index)

    It would be nice if this sort of thing could be done automatically,
    either by the interpreter or a function decorator.
    --
    Michael Hoffman
     
    Michael Hoffman, Jun 13, 2007
    #1
    1. Advertising

  2. Michael Hoffman schrieb:
    > The peephole optimizer now takes things like
    >
    > if 0:
    > do_stuff()
    >
    > and optimizes them away, and optimizes away the conditional in "if 1:".
    >
    > What if I had a function like this?
    >
    > def func(debug=False):
    > for index in xrange(1000000):
    > if debug:
    > print index
    > do_stuff(index)
    >
    > Could the "if debug" be optimized away on function invocation if debug
    > is immutable and never reassigned in the function? When performance
    > really matters in some inner loop, I usually move the conditional
    > outside like this:
    >
    > def func(debug=False):
    > if debug:
    > for index in xrange(1000000):
    > print index
    > do_stuff(index)
    > else:
    > for index in xrange(1000000):
    > do_stuff(index)
    >
    > It would be nice if this sort of thing could be done automatically,
    > either by the interpreter or a function decorator.


    Just use the builtin __debug__ variable for that purpose.
    __debug__ is 'True' if Python is run normally, and 'False'
    if run with the '-O' or '-OO' command line flag.
    The optimizer works in the way you describe above (which
    it will not if you use a custom variable).

    Thomas
     
    Thomas Heller, Jun 13, 2007
    #2
    1. Advertising

  3. Thomas Heller wrote:

    > Just use the builtin __debug__ variable for that purpose.
    > __debug__ is 'True' if Python is run normally, and 'False'
    > if run with the '-O' or '-OO' command line flag.
    > The optimizer works in the way you describe above (which
    > it will not if you use a custom variable).


    Thanks, I didn't know that __debug__ was optimized like this. But that
    was really just a specific example of the general case.
    --
    Michael Hoffman
     
    Michael Hoffman, Jun 13, 2007
    #3
    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. Guest
    Replies:
    5
    Views:
    354
    Guest
    Jul 28, 2004
  2. Ben Fidge

    Optimizing ViewState

    Ben Fidge, Feb 17, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    431
    =?Utf-8?B?U2hhdW4=?=
    Feb 18, 2005
  3. Alejandro Penate-Diaz

    optimizing DropDownLists

    Alejandro Penate-Diaz, Apr 8, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    333
    Karl Seguin
    Apr 8, 2005
  4. octavsly
    Replies:
    0
    Views:
    627
    octavsly
    Apr 25, 2009
  5. Me
    Replies:
    2
    Views:
    247
Loading...

Share This Page