简体   繁体   中英

how does python 3.2 handle function calls from loops?

I am not worried about functions defined by me, but built-in functions or esp. ones from imported modules. Basically, do these pieces of advice still apply?

http://wiki.python.org/moin/PythonSpeed/PerformanceTips#Avoiding_dots...

http://wiki.python.org/moin/PythonSpeed/PerformanceTips#Local_Variables

I think the JIT of PyPy could have helped me here too, but I need to run the thing on a Linux cluster (and my source is for Py3k already).

Yes. The exact version doesn't matter much. These optimizations all boil down to minimizing the use of language features with semantics that prohobit simple and efficient implementations for the general case (which is all an interpreter cares about, while a JIT-compiler can generate better code for specific cases). Specifically:

  • Attribute lookup still follows a pretty fancy algorithm, eg making hashtable lookups (those are amortized O(1), but big O isn't everything), walking up longish (well, not so much in the case of builtins) inheritance chains and invoking other descriptors (properties, getting bound methods). Variable (especially local, see below) lookup is dead simple in comparision, and requires fewer bytecode instructions unless the object in question is already on the top of the stack.
  • Local variables can still be enumerated at compiletime (allowing somewhat efficient implementation using a stack), while globals can still be added and removed at all times, by anyone with a reference to the module object, and even dynamically using string (requring using hashtable for them).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM