472,985 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

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
7 6315
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: myang | last post by:
How can I know a module take how much time? Thanks for any hints. Regards, Yang
2
by: Michal Kwiatkowski | last post by:
Hi! I was just wondering... Python 2.3.5 (#2, Mar 6 2006, 10:12:24) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import timeit >>> a =...
5
by: kpp9c | last post by:
Hi, I was looking at python & datetime and hoping that it would already have a method/func to translate time formats. I need to translate seconds to hh:mm:ss.ms and vice versa and would like...
5
by: yinglcs | last post by:
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...
3
by: jm.suresh | last post by:
I am trying to measure the time the processor spends on some operation, and I want this measure to not depend on the current load of the machine. But doing the following prints different values...
12
by: jeremito | last post by:
Please excuse me if this is obvious to others, but I can't figure it out. I am subclassing dict, but want to prevent direct changing of some key/value pairs. For this I thought I should override...
27
by: idoerg | last post by:
Hi all, I am running Python 2.5 on Feisty Ubuntu. I came across some code that is substantially slower when in a method than in a function. ################# START SOURCE ############# # The...
10
by: Dick Moores | last post by:
I'm still trying to understand classes. I've made some progress, I think, but I don't understand how to use this one. How do I call it, or any of its functions? It's from the Cookbook, at...
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.