473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to use time.clock() function in python

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

Jan 22 '07 #1
5 5617
On Mon, 22 Jan 2007 14:05:16 -0800, yi*****@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.

Jan 22 '07 #2
At Monday 22/1/2007 19:05, yi*****@gmail.com wrote:
>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/pyt...ry/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

Jan 22 '07 #3
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.

Gabriel Genellina wrote:
At Monday 22/1/2007 19:05, yi*****@gmail.com wrote:
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/pyt...ry/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
Jan 22 '07 #4
On Mon, 22 Jan 2007 15:32:58 -0800, samuel.y.l.cheung wrote:
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")

--
Steven D'Aprano

Jan 23 '07 #5
yi*****@gmail.com <yi*****@gmail.comwrote:
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
>>from time import clock, time
print clock(), time()
0.01 1169573460.96
>>print clock(), time()
0.01 1169573463.76
>>print clock(), time()
0.01 1169573467.09
>>>
However running the same under windows you get a quite different
result :-
>>from time import clock, time
print clock(), time()
7.54285810068e-006 1169574534.84
>>print clock(), time()
3.32073322168 1169574538.16
>>print clock(), time()
7.32428004118 1169574542.15
>>>
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.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jan 23 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Szabolcs Nagy | last post by:
I have to measure the time of a while loop, but with time.clock i always get 0.0s, although python manual sais: "this is the function to use for benchmarking Python or timing algorithms" So i...
6
by: cournape | last post by:
Hi there, I have some scientific application written in python. There is a good deal of list processing, but also some "simple" computation such as basic linear algebra involved. I would like to...
5
by: Sinan Nalkaya | last post by:
hello, i need a function like that, wait 5 seconds: (during wait) do the function but function waits for keyboard input so if you dont enter any it waits forever. i tried time.sleep() but when...
17
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
5
by: Tobiah | last post by:
The manual says: On Unix, return the current processor time as a floating point number expressed in seconds. So I ran this program: #!/usr/bin/python import time
5
by: raybakk | last post by:
Hi there. If I make a function in c (I acually use gnu right now), is there any way to find out how many clocksycluses that function takes? If I divide some numbers etc Var1 = Var2/Var3, is it...
18
by: Giovanni Bajo | last post by:
Hello, I experimented something very strange, a few days ago. I was debugging an application at a customer's site, and the problem turned out to be that time.clock() was going "backwards", that...
9
by: Ron Adam | last post by:
I'm having some cross platform issues with timing loops. It seems time.time is better for some computers/platforms and time.clock others, but it's not always clear which, so I came up with the...
8
by: Theo v. Werkhoven | last post by:
hi, In this code I read out an instrument during a user determined period, and save the relative time of the sample (since the start of the test) and the readback value in a csv file. #v+...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.