Packing data

Erno Kuusela erno-news at erno.iki.fi
Thu May 2 17:31:53 EDT 2002


In article <MPG.173b43566b725d129896d2 at news.nwlink.com>, Jeff Shannon
<jeff at ccvcorp.com> writes:

| In article <kuk7qm8r3z.fsf at lasipalatsi.fi>, Erno Kuusela says...
|| % python ding.py
|| <function listcomp_test at 1482a4> 6.82
|| <function map_test at 1482d4> 1.83
|| python ding.py  17.44s user 0.16s system 98% cpu 17.897 total
|| 
|| the map version is also more readable, imho.

| Hm, I'm slightly surprised that there's that significant of a 
| difference in this case.  Just goes to further reinforce that 
| performance needs to be measured, not guessed at.  :)

also to reinforce that doing benchmarks without mentioning python version
numbers is not very informative - the above was on a solaris machine
at work, but when i tried it at home i got something quite different:

% for pv in 2.0 2.1 2.2; do time /usr/bin/python$pv ding.py; done
<function listcomp_test at 0x8097dec> 3.37
<function map_test at 0x809aca4> 1.03
/usr/bin/python$pv ding.py  4.17s user 0.31s system 99% cpu 4.485 total
<function listcomp_test at 0x80d3b9c> 2.31
<function map_test at 0x8108534> 0.61
/usr/bin/python$pv ding.py  2.84s user 0.15s system 102% cpu 2.925 total
<function listcomp_test at 0x814763c> 1.67
<function map_test at 0x8153b14> 0.57
/usr/bin/python$pv ding.py  2.08s user 0.24s system 100% cpu 2.317 total

i think i remember reading that part of the speed disadvantage of list
comprehensions was that map checked the size of the passed-in sequence(s)
and allocated a suitably sized list for results in one go, whereas
list comprehensions used to loop over the sequence and grow the list
dynamically. maybe this is what has changed...

the other speed advantage of map is of course that it doesn't
run any bytecode instructions between "iterations", unlike a list
comprehension.

  -- erno



More information about the Python-list mailing list