How to use time.clock() function in python

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Mon Jan 22 17:28:06 EST 2007


On Mon, 22 Jan 2007 14:05:16 -0800, yinglcs at gmail.com wrote:

> Hi,
> 
> I am following this python example trying to time how long does an
> operation takes, like this:
> 
> My question is why the content of the file (dataFile) is just '0.0'?
> I have tried "print >>dataFile, timeTaken" or "print >>dataFile,str(
> timeTaken)", but gives me 0.0.
> Please tell me what am I missing?
> 
> 
>         t1 = time.clock()
>         os.system(cmd)
> 
>         outputFile = str(i) + ".png"
> 
>         t2 = time.clock()
> 
>         timeTaken = t2 - t1
>         allTimeTaken += timeTaken
>         print >>dataFile, timeTaken



For the correct way to time operations, see the timeit module.

For your specific problem, it is hard to tell what you are doing wrong
when you don't tell us what "datafile" is. What's "outfile" for? It gets
created *after* the command runs, but doesn't get used. 


-- 
Steven.




More information about the Python-list mailing list