How to use time.clock() function in python

Y

yinglcs

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
 
S

Steven D'Aprano

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.
 
G

Gabriel Genellina

At said:
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

time.clock() may not give you enough precision; see this recent post
http://mail.python.org/pipermail/python-list/2007-January/422676.html
Use the timeit module instead.


--
Gabriel Genellina
Softlab SRL






__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 
S

samuel.y.l.cheung

Thanks.

I have a fuction called 'func1'.

def func1:
# logic of the function

When my script just call 'func1()' it works.
func1()

But when put it under timerit.Timer, like this:
t = timeit.Timer("func1()","")
t.repeat(1, 10)

# want to time how long it takes to run 'func1' 10 times, I get an
error like this:
File "/usr/lib/python2.4/timeit.py", line 188, in repeat
t = self.timeit(number)
File "/usr/lib/python2.4/timeit.py", line 161, in timeit
timing = self.inner(it, self.timer)
File "<timeit-src>", line 6, in inner
NameError: global name 'func1' is not defined

I don't understand why i can't find 'func1', when I call the function
'func1' directly, it works.
but why when I call it within 'timeit', it can't find it?

Thank you.
 
S

Steven D'Aprano

File "/usr/lib/python2.4/timeit.py", line 188, in repeat
t = self.timeit(number)
File "/usr/lib/python2.4/timeit.py", line 161, in timeit
timing = self.inner(it, self.timer)
File "<timeit-src>", line 6, in inner
NameError: global name 'func1' is not defined

I don't understand why i can't find 'func1', when I call the function
'func1' directly, it works.
but why when I call it within 'timeit', it can't find it?

Because the code in timeit is running in a different namespace. You have
to import your function first. That's what the setup parameter is used for.

Here's the hard way:

t = timeit.Timer("func1()", """def func1():
#do something here
return result
""")


Here's the easy way:

t = timeit.Timer("func1()", "from __main__ import func1")
 
N

Nick Craig-Wood

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

Under unix, time.clock() measures CPU time used by the current
process. os.system() will consume almost zero CPU while it waits for
the command you ran to finish.

You probably want time.time(), eg

However running the same under windows you get a quite different
result :-

In windows clock() counts in real time and at much higher resolution
than time().

Under windows time() counts in 1ms steps wheras it usually counts in
1us steps under linux.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,217
Latest member
topweb3twitterchannels

Latest Threads

Top