### Thread: how can i calculate time to run a task ???

1. No Profile Picture
NPT
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2005
Posts
16
Rep Power
0

#### how can i calculate time to run a task ???

how can i calculate the time to run my function in Python? Can you help me, thanks .
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2004
Posts
394
Rep Power
52
Hi!

Code:
```import time

def your_function():

loops = 10000    # how often your function is called
begin = time.time()
for x in range(loops):
your_function()
print "%0.3fs" % (time.time() - begin)```
Regards, mawe
3. No Profile Picture
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
Nov 2004
Location
There where the rabbits jump
Posts
556
Rep Power
11
yes and to give alittle feedback...

mutliply the total time for one function timing times the number you want to run it and then you have the total time left if you subtract the time you already did....
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2004
Posts
394
Rep Power
52
monkeyman, maybe it's because english is not my mother tongue, but I have no idea what you mean
5. No Profile Picture
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
Nov 2004
Location
There where the rabbits jump
Posts
556
Rep Power
11
Okay I meant that your way is to see how long it takes to run all of them and mine is how long it takes to run one and then calculate the rest of the time or something like that...

I am not mother tongue either

Code:
```>>> def func(times,function,*args):
start = time.clock()
function(args)
end = time.clock()
Time = "%f" % (end - start)
print str(float(Time)*times)+"s"```
Thats my code for this thing and I did not run all of them I only ran it once...

It probably does not work to great not very accurate but i hope you get my point...
Last edited by monkeyman23555; July 15th, 2005 at 12:16 PM.
6. I would also suggest using time.time() instead of time.clock() for improved compatibility between platforms. It also give better results IMO and is easier to understand .

Mark.

• CyBerHigh agrees
• xlordt agrees
• LinuxPenguin agrees
7. Originally Posted by netytan
I would also suggest using time.time() instead of time.clock() for improved compatibility between platforms. It also give better results IMO and is easier to understand .

Mark.
Not only that.. but time.time( ) works alot faster then time.clock( ) as they are for different purpose's
8. If you check the Python Library Reference, you will find that the Profiler module has classes and functions for this purpose; for a single function, you probably can use timeit.Timer, while for more detailed profiling you should use profile.run() and pstats.Stats.
Last edited by Schol-R-LEA; July 18th, 2005 at 02:40 AM.
9. No Profile Picture
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
Nov 2004
Location
There where the rabbits jump
Posts
556
Rep Power
11
hi about the time.clock I just used it because the other time.time did not work???

I don't know why...