Re: efficiency question
- From: diffuser78@xxxxxxxxx
- Date: 30 Jun 2006 11:16:34 -0700
So nice to know this that you can compare your own code using this
methodology. I was completely unaware of this. Thank you so much.
Fredrik Lundh wrote:
David Harvey wrote:
Suppose I write
if x in ("abc", "def", "xyz"):
doStuff()
elif x in ("pqr", "tuv", "123"):
doOtherStuff()
elif ...
When is python building the tuples? Does it need to build the tuple
every time it comes through this code? Or does it somehow recognise
that they are constant and cache them?
when in doubt, ask the compiler:
def code(x):
if x in ("abc", "def", "xyz"):
doStuff()
elif x in ("pqr", "tuv", "123"):
doOtherStuff()
import dis
dis.dis(code)
prints:
2 0 LOAD_FAST 0 (x)
3 LOAD_CONST 7 (('abc', 'def', 'xyz'))
6 COMPARE_OP 6 (in)
9 JUMP_IF_FALSE 11 (to 23)
12 POP_TOP
3 13 LOAD_GLOBAL 1 (doStuff)
16 CALL_FUNCTION 0
19 POP_TOP
20 JUMP_FORWARD 25 (to 48)
>> 23 POP_TOP
4 24 LOAD_FAST 0 (x)
27 LOAD_CONST 8 (('pqr', 'tuv', '123'))
30 COMPARE_OP 6 (in)
33 JUMP_IF_FALSE 11 (to 47)
36 POP_TOP
5 37 LOAD_GLOBAL 2 (doOtherStuff)
40 CALL_FUNCTION 0
43 POP_TOP
44 JUMP_FORWARD 1 (to 48)
>> 47 POP_TOP
>> 48 LOAD_CONST 0 (None)
51 RETURN_VALUE
so the answer is "yes, in this specific case".
(The tuples I have in mind are of course much longer than three
elements)
in that case, you should probably use pre-calculated sets instead of tuples.
</F>
.
- References:
- Re: efficiency question
- From: Fredrik Lundh
- Re: efficiency question
- Prev by Date: Re: Import bug: Module executed twice when imported!
- Next by Date: Re: sum fonction in gadfly
- Previous by thread: Re: efficiency question
- Next by thread: Re: efficiency question
- Index(es):
Relevant Pages
|