How can I time a method of a class in python using Timeit

S

silverburgh.meryl

Hi,

I am using timeit to time a global function like this

t = timeit.Timer("timeTest()","from __main__ import timeTest")
result = t.timeit();


But how can i use timeit to time a function in a class?
class FetchUrlThread(threading.Thread):
def aFunction(self):
# do something ....

def run(self):
# how can I time how long does aFunction() take here?
aFunction();

Thank you.
 
7

7stud

Hi,

I am using timeit to time a global function like this

t = timeit.Timer("timeTest()","from __main__ import timeTest")
result = t.timeit();

But how can i use timeit to time a function in a class?
class FetchUrlThread(threading.Thread):
def aFunction(self):
# do something ....

def run(self):
# how can I time how long does aFunction() take here?
aFunction();

Thank you.

How about this:

class Dog(object):
def run(self):
result = 10 * 20 + 3

import timeit

t = timeit.Timer("d.run()", "from __main__ import Dog; d = Dog()")
print t.timeit()
 
7

7stud

How about this:

class Dog(object):
def run(self):
result = 10 * 20 + 3

import timeit

t = timeit.Timer("d.run()", "from __main__ import Dog; d = Dog()")
print t.timeit()

Actually, you could do this:

class Dog(object):
def aFunction(self):
result = 20 + 2
def run(self):
#do stuff
aFunction()
#do other stuff
import timeit

t = timeit.Timer("d.aFunction()", "from __main__ import Dog; d =
Dog()")
print t.timeit()


It doesn't matter if you call aFunction() directly if all you want to
do is time aFunction().
 
7

7stud

Actually, you can do this:

class Dog(object):
def aFunction(self):
result = 20 + 2
def run(self):
#do stuff
aFunction()
#do other stuff
import timeit

t = timeit.Timer("d.aFunction()", "from __main__ import Dog; d =
Dog()")
print t.timeit()


Since you only want to time aFunction(), you can call it directly.
 
S

silverburgh.meryl

Actually, you can do this:

class Dog(object):
def aFunction(self):
result = 20 + 2
def run(self):
#do stuff
aFunction()
#do other stuff
import timeit

t = timeit.Timer("d.aFunction()", "from __main__ import Dog; d =
Dog()")
print t.timeit()

Since you only want to time aFunction(), you can call it directly.

Thanks for all the idea.
 
S

silverburgh.meryl

Actually, you can do this:

class Dog(object):
def aFunction(self):
result = 20 + 2
def run(self):
#do stuff
aFunction()
#do other stuff
import timeit

t = timeit.Timer("d.aFunction()", "from __main__ import Dog; d =
Dog()")
print t.timeit()

Since you only want to time aFunction(), you can call it directly.

Can 't = timeit.Timer()' run inside a thread?
And I have multiple threads running this 't = timeit.Time()' function?
 
7

7stud

Can 't = timeit.Timer()' run inside a thread?
And I have multiple threads running this 't = timeit.Time()' function?

__main__ is a thread isn't it? However, timeit() runs in it's own
scope, so you need to import anything you need into that scope using
the setup parameter for the Timer() constructor.
 
7

7stud

Can 't = timeit.Timer()' run inside a thread?
And I have multiple threads running this 't = timeit.Time()' function?

Are you seeing an error like this:

Exception in thread Thread-1:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.4//lib/
python2.4/threading.py", line 442, in __bootstrap
self.run()
File "test1.py", line 34, in run
print t.timeit()
File "/Library/Frameworks/Python.framework/Versions/2.4//lib/
python2.4/timeit.py", line 161, in timeit
timing = self.inner(it, self.timer)
File "<timeit-src>", line 3, in inner
TypeError: __init__() takes exactly 2 arguments (1 given)


That was produced by this code:

----------
import time
import timeit
import threading


t = timeit.Timer("inst.f()", "from __main__ import Dog; inst=Dog()")
print t

class Dog(threading.Thread):

def __init__(self, id):
self.id = id
threading.Thread.__init__(self)

def f(self):
result = 20 + 3

def run(self):
print t
print t.timeit()


d = Dog("d")
d.start()
--------------

I can't explain that error. This works:

--------------
import time
import timeit
import threading


t = timeit.Timer("inst.f()", "from __main__ import Dog; inst=Dog()")

class Dog(threading.Thread):

def f(self):
result = 20 + 3

def run(self):
print t.timeit()

d = Dog()
d.start()
-----------
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top