473,771 Members | 2,297 Online
Bytes | Software Development & Data Engineering Community
+ 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("c allFunc(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 5527
En Sat, 28 Apr 2007 15:48:11 -0300, si************* **@gmail.com
<si************ ***@gmail.comes cribió:
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("c allFunc(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("c allFunc(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(l ine, i)"
t1 = timeit.Timer(st mt, 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.a r>
wrote:
En Sat, 28 Apr 2007 15:48:11 -0300, silverburgh.me. ..@gmail.com
<silverburgh.me ...@gmail.comes cribió:
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("c allFunc(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("c allFunc(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("c allFunc(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(l ine, i)"
t1 = timeit.Timer(st mt, 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.comwr ote:
On Apr 28, 3:37 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Sat, 28 Apr 2007 15:48:11 -0300, silverburgh.me. ..@gmail.com
<silverburgh.me ...@gmail.comes cribió:
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("c allFunc(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("c allFunc(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("c allFunc(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(l ine, i)"
t1 = timeit.Timer(st mt, 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
3551
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: python test.py -t and then, i may get "-t" within test.py for later use.
2
4725
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 to interact with many objects in that DLL and wrapper by reference. (So the classes inherit from MBRO.) While my app is running, I need to obtain info from objects in the 2nd app domain frequently/speedily and I need update the GUI; and I need to...
2
9622
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 creates a new Handler, which is calling another method. Is it possible to give this method an argument? Code now: timer.Elapsed += new ElapsedEventHandler(requestStat);
2
2078
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 array of objs a=document.createElement("a"); a.onclick=eval('function (){ cancellaCal(' + evt.periodi.id_evt_cal
1
2134
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( setup="from Createlst import createlst", stmt=
1
6000
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. now when ever client clicks on the add to cart button than i want the item id no in the button's click event's how can be it possible. Here i show the code System.Web.UI.WebControls.ImageButton add = new...
2
53720
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
7414
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
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9910
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.