The CPython bytecode difference between iteration and loopingAugust 12, 2012
Because I feel like it, today I'm going to show you the difference in CPython bytecodes between the two variants of 'repeat N times' that I talked about in my entry on the periodic strangeness of idiomatic Python. Both are going to be in functions (because that's the common case of where Python code is). First, the iteration version:
The core bytecode of the function looks like the following (note that I have simplified the actual bytecode disassembly to make it easier to read): 0 SETUP_LOOP 1 LOAD_GLOBAL range 2 LOAD_FAST max 3 CALL_FUNCTION 4 GET_ITER 5 FOR_ITER (to 8) 6 STORE_FAST _ 7 JUMP_ABSOLUTE (to 5) 8 POP_BLOCK Bytecodes 1 through 3 are the initial setup, ie the call to (When reading the bytecode, it helps to know that CPython bytecode
is stack based; arguments to operations are put on the stack (eg,
by various LOAD instructions) and then popped off as part of other
operations (eg by STORE instructions). The The explicit loop version is:
There is a bunch more bytecode here: 0 LOAD_CONST 0 1 STORE_FAST i 2 SETUP_LOOP 3 LOAD_FAST i 4 LOAD_FAST max 5 COMPARE_OP < 6 POP_JUMP_IF_FALSE (to 12) 7 LOAD_FAST i 8 LOAD_CONST 1 9 INPLACE_ADD 10 STORE_FAST i 11 JUMP_ABSOLUTE (to 3) 12 POP_BLOCK Setup takes only two bytecodes, to initialize Now, you might sensibly ask if this difference in how many bytecodes
there are makes any real performance difference; after all, an efficient
interpreter can run bytecodes pretty fast. The short answer is that
it does; on a 64-bit Linux machine with Python 2.7.3, the loop based
version is around 3.4 times slower than the iteration based version. I
can make the difference even bigger by changing (I timed with a Does this performance difference matter? Probably not. In real code you're likely to be looping only for a relatively short number of iterations and the real work you're doing on each iteration will probably dwarf the loop overhead. PS: due to a comment by David B on the original entry, the most efficient way to do this is:
This beats even the |
These are my WanderingThoughts GettingAround This is part of CSpace, and is written by ChrisSiebenmann. * * * Atom feeds are available; see the bottom of most pages. Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web |