Populating a dictionary, fast

MrJean1 MrJean1 at gmail.com
Mon Nov 12 18:40:06 EST 2007


On MacOS** your version

#!/usr/bin/python
v = {}
for line in open('keys.txt'):
    v[long(line.strip())] = True

using these <http://www.sendspace.com/file/9530i7> keys takes

    24.9 secs with ActivePython 2.5.1
and
    212.3 secs with Apple's Python 2.3.5.


However, this version

#!/usr/bin/python
v = {}
for line in open('keys.txt'):
    v[line] = True

takes
    16.5 secs with ActivePython 2.5.1
and
    18.7 secs with Apple's Python 2.3.5.


/Jean Brouwers

**) MacOS X 10.4.10 on Mini Mac, 1.83 GHz Intel Core Duo, 2 GB RAM.




More information about the Python-list mailing list