<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:times new roman,new york,times,serif;font-size:12pt">For larger blocks of code, cProfile may also be useful for speed tests.<br><div>&nbsp;</div>Cheers!!<br>Albert-Jan<br><br><div>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>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?<br>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<div><br></div><div style="font-family: times new roman,new york,times,serif; font-size: 12pt;"><br><div style="font-family: arial,helvetica,sans-serif; font-size: 13px;"><font size="2" face="Tahoma"><hr size="1"><b><span style="font-weight: bold;">From:</span></b> Steven D'Aprano &lt;steve@pearwood.info&gt;<br><b><span style="font-weight:
 bold;">To:</span></b> tutor@python.org<br><b><span style="font-weight: bold;">Sent:</span></b> Tue, November 9, 2010 9:54:24 PM<br><b><span style="font-weight: bold;">Subject:</span></b> Re: [Tutor] List comprehension question<br></font><br>
Richard D. Moores wrote:<br><br><span>&gt; See &lt;<a target="_blank" href="http://tutoree7.pastebin.com/R82876Eg">http://tutoree7.pastebin.com/R82876Eg</a>&gt; for a speed test with n =</span><br>&gt; 100,000 and 100,000 loops<br><br>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.<br><br>Fortunately Python comes with that battery already included: the timeit module. You can use it from the shell like this:<br><br>python -m timeit -s "setup code goes here" "test code goes here"<br><br>At the Python interactive interpreter, the most useful pattern I've found is:<br><br>from timeit import Timer<br>t = Timer("proper_divisors(n)",<br>&nbsp; &nbsp; "from __main__ import proper_divisors; n = 100000")<br>min(t.repeat(repeat=5, number=100000))<br><br><br>Or if you're happy with Python's
 defaults, that last line can be just:<br><br>min(t.repeat())<br><br><br><br>-- Steven<br>_______________________________________________<br>Tutor maillist&nbsp; -&nbsp; <a ymailto="mailto:Tutor@python.org" href="mailto:Tutor@python.org">Tutor@python.org</a><br>To unsubscribe or change subscription options:<br><span><a target="_blank" href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a></span><br></div></div></div>
</div><br>

      </body></html>