How to pass arguments to the function embedded in the timeit.Timer()

D

Dongsheng Ruan

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=
"createlst(%s)"%(x) )

But how can I extend it to two or more arguments?

Like this:

p1=createlst.createlst(10000)
p2=createlst.createlst(10000)
mytime = timeit.Timer(setup="from list_concat_copy import list_concat_copy",
stmt="list_concat_copy.list_concat_copy(%x,%y)"%p1,p2 )

I don't know how to end the timeit.Timer. Should it be (%x,%y)"%p1,p2 or
(%x,%y)"%p1,%p2 or (%x,%y)"(%p1%p2) .

I tried and none worked. I just got error message like global variable "A'
not defined.

Can anybody help?

Thanks!
 
G

Gabriel Genellina

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=
"createlst(%s)"%(x) )

This is the % operator for strings, and it's general, not related to
the timeit module.
You can find it explained in almost every tutorial. The "official"
tutorial explains it here:
<http://docs.python.org/tut/node9.html#formatting> and for more
detailed usage see here:
<http://docs.python.org/lib/typesseq-strings.html>

If you haven't already done it, I strongly recommend reading the
Python tutorial (you should find it inside your Python installation,
and you can read it online at <http://docs.python.org/tut/> )


--
Gabriel Genellina
Softlab SRL






__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top