Re: [Python-Dev] python optimization
Neal Becker wrote: > One possible way to improve the situation is, that if we really believe > python cannot easily support such optimizations because the code is too > "dynamic", is to allow manual annotation of functions. For example, gcc > has allowed such annotations using __attribute__ for quite a while. This > would allow the programmer to specify that a variable is constant, or that > a function is pure (having no side effects). Raymond's constant binding decorator comes pretty close to achieving that: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/277940 It kicks in when the def statement is executed, rather than when the module is compiled, but that difference is not uncommon in Python (you don't know what half the names refer to until the top-level of the module is executed). Cheers, Nick. -- Nick Coghlan | [EMAIL PROTECTED] | Brisbane, Australia --- http://boredomandlaziness.blogspot.com ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] python optimization
One possible way to improve the situation is, that if we really believe python cannot easily support such optimizations because the code is too "dynamic", is to allow manual annotation of functions. For example, gcc has allowed such annotations using __attribute__ for quite a while. This would allow the programmer to specify that a variable is constant, or that a function is pure (having no side effects). ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] python optimization
[Neal Becker] > >>I don't know to what extent these kind of optimizations are available to > >>cpython. For example, are constant calculations removed from loops? [Brett Cannon] > > If you mean ``2+3``, then yes. [Greg Ewing] > Actually, no. Constant folding *could* be done, but it currently isn't: > > >>> def f(): > ... return 2+3 > ... > >>> import dis > >>> dis.dis(f) >2 0 LOAD_CONST 1 (2) >3 LOAD_CONST 2 (3) >6 BINARY_ADD >7 RETURN_VALUE >8 LOAD_CONST 0 (None) > 11 RETURN_VALUE That looks like a disassembly from the ancient and primitive Py2.3 ;-) It looks a little different in the ahead-of-its-time Py2.5 alpha: Python 2.5a0 (#46, Sep 15 2005, 00:51:34) [MSC v.1200 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> def f(): return 2+3 >>> import dis >>> dis.dis(f) 2 0 LOAD_CONST 3 (5) 3 RETURN_VALUE Raymond ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] python optimization
Brett Cannon wrote: >>I don't know to what extent these kind of optimizations are available to >>cpython. For example, are constant calculations removed from loops? > > If you mean ``2+3``, then yes. Actually, no. Constant folding *could* be done, but it currently isn't: >>> def f(): ... return 2+3 ... >>> import dis >>> dis.dis(f) 2 0 LOAD_CONST 1 (2) 3 LOAD_CONST 2 (3) 6 BINARY_ADD 7 RETURN_VALUE 8 LOAD_CONST 0 (None) 11 RETURN_VALUE >>> -- Greg Ewing, Computer Science Dept, +--+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | [EMAIL PROTECTED] +--+ ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] python optimization
On 9/15/05, Neal Becker <[EMAIL PROTECTED]> wrote: > 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? If you mean ``2+3``, then yes. If you mean ``2 + a`` where is a loop invariant, then no. > How > about functions? No optimization there. > Is there a significant cost to putting a function def > inside a loop rather than outside? > If you put it in a loop then the function must be reconstructed every time through the loop. I have never benchmarked the cost of function construction but I doubt it's cheap. The problem with all of these optimizations that you can do in more static languages is you get to assume most things do not change from underneath you. In Python, thanks to threading, access to frames, global namespaces of modules, etc., most things cannot be naively optimized without a lot of checking up front to make sure the optimization is valid every time it is run. If you want some gruesome detail, you can read my thesis (http://www.drifty.org/thesis.pdf) and the section on problems with introducing type inference into Python. -Brett ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] python optimization
Hi Neal, I don't believe that cpython currently does any of the optimizations you refer to below. That said, it is very reasonable to adopt "a style of coding that is highly readable, making the assumption that the compiler will do good things" when coding in Python. Python is one of the most highly optimised languages in the world along the Programmer Productivity metric. Line for line, you can pack more readable, obvious, and maintainable meaning into Python than pretty much any other language. The upshot is that then you can profile the final running code and see if it really matters that the compiler is using an extra .034 microseconds. That's my $0.028 US (damn inflation!) On 15/09/05, Neal Becker <[EMAIL PROTECTED]> wrote: I use cpython. I'm accustomed (from c++/gcc) to a style of coding that ishighly readable, making the assumption that the compiler will do good things to optimize the code despite the style in which it's written. Forexample, I assume constants are removed from loops. In general, an entityis defined as close to the point of usage as possible. I don't know to what extent these kind of optimizations are available tocpython. For example, are constant calculations removed from loops? Howabout functions? Is there a significant cost to putting a function def inside a loop rather than outside?--http://mail.python.org/mailman/listinfo/python-list-- "A little government and a little luck are necessary in life, but only a fool trusts either of them." -- P. J. O'Rourke ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] python optimization
On Thu, Sep 15, 2005, Neal Becker wrote: > > 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? This question is about using Python, not improving/fixing Python; please use comp.lang.python (python-list) for these kinds of questions and do not cc python-dev. -- Aahz ([EMAIL PROTECTED]) <*> http://www.pythoncraft.com/ The way to build large Python applications is to componentize and loosely-couple the hell out of everything. ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
[Python-Dev] 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? ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com