473,473 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to pass in argument to timeit.Timer

Hi,

I have a function in my python like this:
def callFunc(line, no):
# some code

And I want to do a performance test like this:
for line in f:
for i in range(int(count)):
t1 = timeit.Timer("callFunc(line, i)","from __main__
import callFunc")
r1 = t1.timeit();
print r1;

but when I run it, it can't recognize the parameter 'line' and 'i',
can you please tell me how to fix it? i get this error:

File "/usr/lib/python2.4/timeit.py", line 161, in timeit
timing = self.inner(it, self.timer)
File "<timeit-src>", line 6, in inner
NameError: global name 'line' is not defined

Thank you.

Apr 28 '07 #1
3 5512
En Sat, 28 Apr 2007 15:48:11 -0300, si***************@gmail.com
<si***************@gmail.comescribió:
I have a function in my python like this:
def callFunc(line, no):
# some code

And I want to do a performance test like this:
for line in f:
for i in range(int(count)):
t1 = timeit.Timer("callFunc(line, i)","from __main__
import callFunc")
r1 = t1.timeit();
print r1;

but when I run it, it can't recognize the parameter 'line' and 'i',
can you please tell me how to fix it? i get this error:
They go in the "setup" parameter, like this:

t1 = timeit.Timer("callFunc(line, i)","from __main__ import callFunc;
line=%r; i=%d" % (line, i))

If it gets much longer, try this:

setup = """
from __main__ import callFunc
line = %r
i = %d""" % (line, i)
stmt = "callFunc(line, i)"
t1 = timeit.Timer(stmt, setup)

--
Gabriel Genellina

PS: Please leave out the final ; - this is Python, not C nor ...
PS2: Perhaps the only place where I've used ; is with timeit. And even
then you can avoid them as in the last example.
Apr 28 '07 #2
On Apr 28, 3:37 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Sat, 28 Apr 2007 15:48:11 -0300, silverburgh.me...@gmail.com
<silverburgh.me...@gmail.comescribió:
I have a function in my python like this:
def callFunc(line, no):
# some code
And I want to do a performance test like this:
for line in f:
for i in range(int(count)):
t1 = timeit.Timer("callFunc(line, i)","from __main__
import callFunc")
r1 = t1.timeit();
print r1;
but when I run it, it can't recognize the parameter 'line' and 'i',
can you please tell me how to fix it? i get this error:

They go in the "setup" parameter, like this:

t1 = timeit.Timer("callFunc(line, i)","from __main__ import callFunc;
line=%r; i=%d" % (line, i))
Thanks I try it:

def stressTest():
for line in f:
for i in range(int(count)):
print i;
t1 = timeit.Timer("callFunc(line, i)","from __main__
import callFunc; line=%r; i=%d" % (line, i))
r1 = t1.timeit();
times.append(r1);
print r1;

But it keeps calling callFunc() and it never returns from my loop. The
value of 'count' is 2.

Thank you.

If it gets much longer, try this:

setup = """
from __main__ import callFunc
line = %r
i = %d""" % (line, i)
stmt = "callFunc(line, i)"
t1 = timeit.Timer(stmt, setup)

--
Gabriel Genellina

PS: Please leave out the final ; - this is Python, not C nor ...
PS2: Perhaps the only place where I've used ; is with timeit. And even
then you can avoid them as in the last example.

Apr 30 '07 #3
On Apr 30, 1:24 pm, "silverburgh.me...@gmail.com"
<silverburgh.me...@gmail.comwrote:
On Apr 28, 3:37 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Sat, 28 Apr 2007 15:48:11 -0300, silverburgh.me...@gmail.com
<silverburgh.me...@gmail.comescribió:
I have a function in my python like this:
def callFunc(line, no):
# some code
And I want to do a performance test like this:
for line in f:
for i in range(int(count)):
t1 = timeit.Timer("callFunc(line, i)","from __main__
import callFunc")
r1 = t1.timeit();
print r1;
but when I run it, it can't recognize the parameter 'line' and 'i',
can you please tell me how to fix it? i get this error:
They go in the "setup" parameter, like this:
t1 = timeit.Timer("callFunc(line, i)","from __main__ import callFunc;
line=%r; i=%d" % (line, i))

Thanks I try it:

def stressTest():
for line in f:
for i in range(int(count)):
print i;
t1 = timeit.Timer("callFunc(line, i)","from __main__
import callFunc; line=%r; i=%d" % (line, i))
r1 = t1.timeit();
times.append(r1);
print r1;

But it keeps calling callFunc() and it never returns from my loop. The
value of 'count' is 2.

Thank you.
If it gets much longer, try this:
setup = """
from __main__ import callFunc
line = %r
i = %d""" % (line, i)
stmt = "callFunc(line, i)"
t1 = timeit.Timer(stmt, setup)
--
Gabriel Genellina
PS: Please leave out the final ; - this is Python, not C nor ...
PS2: Perhaps the only place where I've used ; is with timeit. And even
then you can avoid them as in the last example.
I think I solve the problem by putting a number in timeit() function.
thank you.

Apr 30 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: quiteblack | last post by:
howdy~ i wrote a .py file and it works fine, my goal is to pass argument to that py file when it get executed, and accept that argument within py file, eg. i prefer a command like below: ...
2
by: Mountain Bikn' Guy | last post by:
It is known that one cannot pass arguments as ref or out in a marshal-by-reference class. My problem is that I have a C DLL (and C# wrapper) that I need to isolate in an AppDomain and then I need...
2
by: Thorsten moeller | last post by:
hi, i am am fairly new to c# and trying to write a service to check some thing periodically. i want to use a windows service. The onStart method is starting a timer. The timer.elapsed event...
2
by: fusillo | last post by:
My code pass a value of a variable to the event handler's argument by means of eval statement here's: //statements setting evt for (var i=0; i<evt.periodi.length; i++){ //evt.periodi is an...
1
by: Dongsheng Ruan | last post by:
Hi Does anybody know how to pass multiple arguments to the function tested in timeit.timer() in python? I googled and found how to pass one argument: x=10000 mytime = timeit.Timer(...
1
by: TarDuk a memory beyond the life | last post by:
I am developing a shopping cart application for jwellary designing \ now i am displaying all the product in the table dynamically. and also show on image button as a add to cart after every item....
2
by: ashokd001 | last post by:
Hi I want to pass argument to gdb but i don't know to do that. Say, my executable taking 3 argument. <myexe> <arg1> <arg2> <arg3>
5
dlite922
by: dlite922 | last post by:
All i want to do in C++ is pass a few arguments, concatenate them into variable and call system() I've gotten this far: #include <stdio.h> #include <stdlib.h> #include <string>
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.