[Tutor] List comprehension question

Albert-Jan Roskam fomcl at yahoo.com
Tue Nov 9 22:58:22 CET 2010


For larger blocks of code, cProfile may also be useful for speed tests.

 Cheers!!
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a fresh water system, and public health, what have the 
Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




________________________________
From: Steven D'Aprano <steve at pearwood.info>
To: tutor at python.org
Sent: Tue, November 9, 2010 9:54:24 PM
Subject: Re: [Tutor] List comprehension question

Richard D. Moores wrote:

> See <http://tutoree7.pastebin.com/R82876Eg> for a speed test with n =
> 100,000 and 100,000 loops

As a general rule, you shouldn't try to roll your own speed tests. There are 
various subtleties that can throw your results right out. Timing small code 
snippets on modern multi-tasking computers is fraught with difficulties.

Fortunately Python comes with that battery already included: the timeit module. 
You can use it from the shell like this:

python -m timeit -s "setup code goes here" "test code goes here"

At the Python interactive interpreter, the most useful pattern I've found is:

from timeit import Timer
t = Timer("proper_divisors(n)",
    "from __main__ import proper_divisors; n = 100000")
min(t.repeat(repeat=5, number=100000))


Or if you're happy with Python's defaults, that last line can be just:

min(t.repeat())



-- Steven
_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101109/a28a4a73/attachment-0001.html>


More information about the Tutor mailing list