Array design question

Duncan Booth duncan at NOSPAMrcp.co.uk
Thu May 29 09:21:18 EDT 2003


Peter Slizik <peter.slizik at pobox.sk> wrote in news:bb4oom$5fc19$1 at ID-196014.news.dfncis.de:

> Thanks, I know I can use a dictionary. But I think the usage of a 
> hash-table in the place where 'ordinary' array could be employed is at 
> least unaesthetical. It is also inefficient.

Why do you think it is inefficient? Have you actually tried it and 
found either a performance problem or excessive use of memory, or 
are you just guessing.

A dictionary is actually slower, but for this sort of use its only 
about 20% slower, and unless the dictionary is quite sparse 
there won't be any collisions when using integer indexes.

C:\Python23>lib\timeit.py -s "l = [0] * 100" "l[0] += 1" "l[50] += 1" "l[99] += 1"
1000000 loops, best of 3: 1.74 usec per loop

C:\Python23>lib\timeit.py -s "l = dict([(i,0) for i in range(100)])" "l[0] += 1" "l[50] += 1" "l[99] += 1"
100000 loops, best of 3: 2.11 usec per loop

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list