python optimization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neal Becker

    #1

    python optimization

    I use cpython. I'm accustomed (from c++/gcc) to a style of coding that is
    highly readable, making the assumption that the compiler will do good
    things to optimize the code despite the style in which it's written. For
    example, I assume constants are removed from loops. In general, an entity
    is defined as close to the point of usage as possible.

    I don't know to what extent these kind of optimizations are available to
    cpython. For example, are constant calculations removed from loops? How
    about functions? Is there a significant cost to putting a function def
    inside a loop rather than outside?

  • David Wilson

    #2
    Re: python optimization

    For the most part, CPython performs few optimisations by itself. You
    may be interested in psyco, which performs several heavy optimisations
    on running Python code.



    Defining a function inside a loop in CPython will cause a new function
    object to be created each and every time the loop runs. No such
    automatic optimisation is performed there. For the most part, this lack
    of opimisation not only simplifies the CPython implementation, but also
    causes code to act much more closely to how it was defined, which is
    good for new and advanced users alike.

    Other than psyco, IronPython and PyPy are two projects which you might
    be interested in if execution performance is of interest to you.





    David.

    Comment

    • Reinhold Birkenfeld

      #3
      Re: python optimization

      David Wilson wrote:[color=blue]
      > For the most part, CPython performs few optimisations by itself. You
      > may be interested in psyco, which performs several heavy optimisations
      > on running Python code.
      >
      > http://psyco.sf.net/
      >
      > Defining a function inside a loop in CPython will cause a new function
      > object to be created each and every time the loop runs. No such
      > automatic optimisation is performed there. For the most part, this lack
      > of opimisation not only simplifies the CPython implementation, but also
      > causes code to act much more closely to how it was defined, which is
      > good for new and advanced users alike.[/color]

      More importantly, since Python supports lexical scopes, a function defined
      in a loop could be different each time it is defined, e.g.

      def getadders(to):
      for i in range(to):
      def adder(amount):
      return i + amount
      yield adder


      Reinhold

      Comment

      • Thomas Heller

        #4
        Re: python optimization

        Reinhold Birkenfeld <reinhold-birkenfeld-nospam@wolke7.n et> writes:
        [color=blue]
        > David Wilson wrote:[color=green]
        >> For the most part, CPython performs few optimisations by itself. You
        >> may be interested in psyco, which performs several heavy optimisations
        >> on running Python code.
        >>
        >> http://psyco.sf.net/
        >>
        >> Defining a function inside a loop in CPython will cause a new function
        >> object to be created each and every time the loop runs. No such
        >> automatic optimisation is performed there. For the most part, this lack
        >> of opimisation not only simplifies the CPython implementation, but also
        >> causes code to act much more closely to how it was defined, which is
        >> good for new and advanced users alike.[/color]
        >
        > More importantly, since Python supports lexical scopes, a function defined
        > in a loop could be different each time it is defined, e.g.
        >
        > def getadders(to):
        > for i in range(to):
        > def adder(amount):
        > return i + amount
        > yield adder[/color]

        Hehe. Dangerous code.
        [color=blue][color=green][color=darkred]
        >>> def getadders(to):[/color][/color][/color]
        .... for i in range(to):
        .... def adder(amount):
        .... return i + amount
        .... yield adder
        ....[color=blue][color=green][color=darkred]
        >>>
        >>> for f in getadders(3):[/color][/color][/color]
        .... print f(42)
        ....
        42
        43
        44[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        Seems to work. But observe this:
        [color=blue][color=green][color=darkred]
        >>> def getadders(to):[/color][/color][/color]
        .... for i in range(to):
        .... def adder(amount):
        .... return i + amount
        .... yield adder
        ....[color=blue][color=green][color=darkred]
        >>>
        >>> funcs = [x for x in getadders(3)]
        >>> for f in funcs:[/color][/color][/color]
        .... print f(42)
        ....
        44
        44
        44[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        Thomas

        Comment

        • Neal Becker

          #5
          Re: python optimization

          Reinhold Birkenfeld wrote:
          [color=blue]
          > David Wilson wrote:[color=green]
          >> For the most part, CPython performs few optimisations by itself. You
          >> may be interested in psyco, which performs several heavy optimisations
          >> on running Python code.
          >>
          >> http://psyco.sf.net/
          >>[/color][/color]

          I might be, if it supported x86_64, but AFAICT, it doesn't.

          Comment

          Working...