By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

May 24 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On May 24, 9:36 am, "silverburgh.me...@gmail.com"
<silverburgh.me...@gmail.comwrote:
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()

May 24 '07 #2

P: n/a
On May 24, 11:30 am, 7stud <bbxx789_0...@yahoo.comwrote:
On May 24, 9:36 am, "silverburgh.me...@gmail.com"

<silverburgh.me...@gmail.comwrote:
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()
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().

May 24 '07 #3

P: n/a
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.

May 24 '07 #4

P: n/a
On May 24, 12:41 pm, 7stud <bbxx789_0...@yahoo.comwrote:
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.

May 24 '07 #5

P: n/a
On May 24, 12:41 pm, 7stud <bbxx789_0...@yahoo.comwrote:
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?

May 24 '07 #6

P: n/a
On May 24, 12:23 pm, "silverburgh.me...@gmail.com"
<silverburgh.me...@gmail.comwrote:
On May 24, 12:41 pm, 7stud <bbxx789_0...@yahoo.comwrote:
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?
__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.

May 24 '07 #7

P: n/a
On May 24, 12:23 pm, "silverburgh.me...@gmail.com"
<silverburgh.me...@gmail.comwrote:
On May 24, 12:41 pm, 7stud <bbxx789_0...@yahoo.comwrote:
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?
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()
-----------

May 24 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.