How to use timeit when timing a function
Asked Answered
P

5

15

Let me start off by saying I know almost nothing about python but have to write a program in three different languages (already done in java and c++).

I need to be able to time the execution of a method a certain number of times and then print the time it took for the over-all execution time.

I.e.

I have function A (which is performSearch(arrayTest) where arrayTest is an array of known size). A is executed 10 times

I need to be able to time how long it took from before A was executed to after A was executed.

Photophilous answered 25/9, 2013 at 16:49 Comment(0)
M
16

You can read how to use timeit here.

And assuming you have a function called performSearch in the same file that your running timeit from the following would work.

import timeit

def performSearch(array):
    array.sort()


arrayTest = ["X"]*1000

if __name__ == "__main__":
    print(timeit.timeit("performSearch(arrayTest)","from __main__ import performSearch, arrayTest",number=10))

Which returns:

0.000162031766607
Muckraker answered 25/9, 2013 at 16:57 Comment(5)
I am getting a syntax error. here is the full code + what you said to use pastebin.com/71uXny9LPhotophilous
performSearch is your function not functionAMuckraker
change from __main__ import functionA, arrayTest to from __main__ import performSearch, arrayTestMuckraker
When I said functionA i was being generic, with the hope of not needing my entire codePhotophilous
I can understand that, all you need to do is change the bits in mine that say functionA to performSearch.Muckraker
C
10

You could do something like this:

import time

start = time.time()
A()
end = time.time()
print "Took %f ms" % ((end - start) * 1000.0)
Caddric answered 25/9, 2013 at 17:0 Comment(2)
This is a cleaner and nicer way of doing it while achieving practically the same result (at least in my case).Admass
timeit uses a more precise counter, has functionality to do multiple measurements, and switches off garbage collection by defaultRisible
S
3

You can use below code as an example:

import timeit

def string_generator(size):
    return (size/8) * "ABCDEFGH"

if __name__ == "__main__":
    #the below line runs the statement inside of '' for 100 times (number).
    print timeit.timeit('"-".join(str(n) for n in range(100))',number=100)
    #the below line runs the statement inside of '' for 10 times (number) and repeat it 3 times.
    print timeit.repeat('"-".join(str(n) for n in range(100))',repeat=3,number=10)
    #if you would like to time a function, you can do it similar to below example:
    print timeit.timeit("string_generator(2**12)", setup="from __main__ import string_generator")

The results are :

0.00784516334534
[0.0009770393371582031, 0.00036597251892089844, 0.00037407875061035156]
0.414484977722

The unit for the results is second. More examples are exist in python website. enter link description here

Also you can use ipython. The same example is listed below.

In [25]: %timeit "-".join(str(n) for n in range(100))

The result is :

10000 loops, best of 3: 22.9 µs per loop

As you can see, the unit is macro second.

Searching answered 10/12, 2014 at 0:34 Comment(0)
D
2

if you want something simpler

import time
startMillis = int(round(time.time() * 1000))
print startMillis
time.sleep(5) # this is your function that takes time to execute
endMillis = int(round(time.time() * 1000))
print endMillis

timeTaken = endMillis - startMillis
Drizzle answered 25/9, 2013 at 17:0 Comment(0)
T
1

yes well, just time it.

ex

total= 0
for i in range(1000):
    start= time.clock()
    function()
    end= time.clock()
    total += end-start
time= total/1000
Touchstone answered 25/9, 2013 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.