On Thu, 04 Jan 2007 02:10:44 +0100, Michael M. wrote:

> print "\nTiming a 1 million loop 'for loop' ..."
> start = time.clock()
> for x in range(1000000):
>    y = x  # do something

Why not "pass # do nothing"?


> end = time.clock()
> print "Time elapsed = ", end - start, "seconds"

Are you aware that you're timing the CREATION of a rather large list, as
well as the loop?

[snip code]

> #print "".join(pi)
> print pi
> 
> end_prg = time.clock()

and also the time taken for I/O printing the digits? That can be quite
slow.

For timing functions and small snippets of code, you should investigate
the timeit module. It is very flexible, and will often give more accurate
results than roll-you-own timing.

For example:

>>> import timeit
>>> t = timeit.Timer("for i in range(1000000): pass", "pass")
>>> t.timeit(100)  # time the loop one hundred times
29.367985010147095

Now compare it to the speed where you only create the list once.

>>> t = timeit.Timer("for i in L: pass", "L = range(1000000)")
>>> t.timeit(100)
16.155359983444214


-- 
Steven D'Aprano 

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to