looping through the keys of a dictionary

Markus Schaber markus at schabi.de
Sun Aug 26 16:51:54 EDT 2001


Hi,

Alex Martelli <aleax at aleax.it> schrub:

>> seems to be faster in my eyes, as it doesn't have a dictionary lookup
>> every round.
> 
> SEEMS and IS, of course, are often very different, MOST
> particularly in the arena of computer-program performance.
> Have you tried to MEASURE things...?
> 
> File dipe.py:
[...]

> D:\py21>python dipe.py
> using keys: 0.45
> using items: 0.53
> using keys: 0.45
> using items: 0.53
> 
> D:\py21>python dipe.py
> using keys: 0.45
> using items: 0.53
> using keys: 0.45
> using items: 0.53

Using python 1.5.2 on Linux, I get:
schabi at lunix:~/python/dict$ python dict.py
using keys: 0.41
using items: 0.42
using keys: 0.41
using items: 0.4
schabi at lunix:~/python/dict$ python dict.py
using keys: 0.41
using items: 0.42
using keys: 0.4
using items: 0.41

This seems rather the same speed.

However, using python2.0.1, I get nearly the same values as you:
schabi at lunix:~/python/dict$ python2 dict.py
using keys: 0.42
using items: 0.53
using keys: 0.42
using items: 0.53
schabi at lunix:~/python/dict$ python2 dict.py
using keys: 0.42
using items: 0.53
using keys: 0.42
using items: 0.52

This lets me to the following conclusions:
- Your Machine speed seems to be in the 500Mhz Athlon class :-)

- Key Access seems to be faster on Linux than on NT - or dict access 
slower on linux (when your machine is slower than mine)

- Python 2 is slightly slower in dictionary lookup (or general code 
execution), but it needs 25% more time using keys.

So you're right.

markus
-- 
1) Customers cause problems.
2) Marketing is trying to create more customers.
Therefore:
3) Marketing is evil.  (Grand Edwards in comp.lang.python)



More information about the Python-list mailing list