tags:

views:

295

answers:

4

How much of a difference are these two as far as performance?

tmp = []
tmp.append(True)
print tmp[0]

And

tmp = {}
tmp[0] = True
print tmp[0]
+13  A: 

The timeit module in the standard library is designed just to answer such questions! Forget the print (which would have the nasty side effect of spewing stuff to your terminal;-) and compare:

$ python -mtimeit 'tmp=[]; tmp.append(True); x=tmp[0]'
1000000 loops, best of 3: 0.716 usec per loop
$ python -mtimeit 'tmp={}; tmp[0]=True; x=tmp[0]'
1000000 loops, best of 3: 0.515 usec per loop

So, the dict is the winner -- by 0.2 microseconds...!-)

Alex Martelli
Nice. I guess I was worrying over nothing then. Thanks.
Scott
A: 

they are equitable in my testing

Luke Schafer
+1  A: 

Of course there will be a slight difference, but honestly you shouldn't be worrying about this. What you're doing is micro-optimizing your code, which is something you should avoid. In essence, you're wasting time optimizing something that won't make any difference in your application at all, when you could be spending that time doing something worthwhile.

So is there a difference in speed between the two examples you posted? Certainly. Is it going to matter? Nope. :)

musicfreak
Absolutely true. However, we put timeit in the standard library mostly because inevitably programmers WILL wonder about microscopic performance issues -- down to the fraction of microsecond... that's just how programmers ARE (and we know because we, as in "the python core developers", ARE programmers;-). So, answering such questions now takes a minute -- and then one can get on with one's life (and hopefully good software development practices;-).
Alex Martelli
OR you could teach them to think differently, and then you won't hear anymore such questions from them. ;)
musicfreak
Well, I figure since I'm using an interpreted language, I'd try to make up for some of it. I'm just thinking ahead some, because I don't know if it'll be able to handle multiple devices, but I'm designing it for that purpose.
Scott
The POINT of an interpreted language is that you don't have to worry about the performance of tiny details like this, and you can get to actually developing your application faster. ;) I guarantee you, unless you're doing some intense calculations, Python will not be your bottleneck.
musicfreak
That's not always true. If you're programming Lua for an MMO, small performance boosts can have a large improvement in actual game play. But of course Lua isn't Python, and I'm not being fed ticks by the graphics engine. At any rate, it's just a question. I don't think I wasted any time asking it.
Scott
+4  A: 

Not only is micro-optimization usually pointless in general, I find it is especially difficult and arcane for Python in particular. It is very easy to actually make your code simultaneously slower and more complicated. See this Stack Overflow question for an example where the simplest, clearest, and shortest Python solutions also turned out to be the fastest.

As others have shown with actual tests, the speed difference between your two choices is quite small. What is less small is the semantic difference. Lists and dictionaries are not merely two implementations of the same concept, but are intended for different uses. Pick the one which better fits your use.

John Y
Well, this particular case I can use either dict or list. The only advantage one may have over the other is performance, hence the question. Well, the dicts are also cleaner looking. It's clear what index is being assigned. I chose the dict but there was this nagging question.
Scott