Dijkstra on Python

James J. Besemer jb at cascade-sys.com
Sat Aug 17 09:02:55 CEST 2002


gbreed at cix.compulink.co.uk wrote:

> James J. Besemer wrote:
>
> > I mean this strikes me as crazy.  People are saying get rid of lambda
> > and map when they're typically faster than for or while loops and
> > then they praise comprehensions when they're the most unusual
> > and slowest of all.
>
> Another reason is that what you say isn't true:-

I wouldn't have been able to make this claim if I didn't have
data to back it up.

I published some benchmarks here back in April:

    http://mail.python.org/pipermail/python-list/2002-April/101230.html

I would guess that comprehensions fared better in your tests
because you used the most trivial case, while my benchmark
included an "if" clause (which is more representative IMHO).

Also, you measured wall clock time (which is inherently
unreliable) while I compared CPU times.

Ha!

When I run your benchmark on my machine (with clock()
substituted for time()), for 100K iterations comprehensions
again are slowest and lambda the fastest:

    comprehensions   6.1262475223
    for loop         5.81651351121
    map/lambda       5.65582492231

Of course, benchmarks are hard to design to produce
meaningful and fair data and relatively easy to fudge.
Assuming the clock() vs. time() substitution isn't too
gross an error, then you evidently have a machine
about twice as fast as mine, so that too may affect
benchmark results.

Therefore, I will concede that (per my earlier data)
comprehensions and loops are about the same.  It's
map/lambda that can be remarkably faster.

I'll further concede that differences in runtime performance
(on this small scale) are irrelevant to much of what Pythonites do.

I still think it's the height of hypocrisy to say list
comprehensions don't bloat the language but do-while would.

Also I think it would be silly to remove map/lambda.

Regards

--jb

--
James J. Besemer  503-280-0838 voice
http://cascade-sys.com  503-280-0375 fax
mailto:jb at cascade-sys.com






More information about the Python-list mailing list