How to pass in argument to timeit.Timer

Discussion in 'Python' started by silverburgh.meryl@gmail.com, Apr 28, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. En Sat, 28 Apr 2007 15:48:11 -0300,
    <> escribió:

    > 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.
     
    Gabriel Genellina, Apr 28, 2007
    #2
    1. Advertising

  3. Guest

    On Apr 28, 3:37 pm, "Gabriel Genellina" <>
    wrote:
    > En Sat, 28 Apr 2007 15:48:11 -0300,
    > <> escribió:
    >
    > > 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, 2007
    #3
  4. Guest

    On Apr 30, 1:24 pm, ""
    <> wrote:
    > On Apr 28, 3:37 pm, "Gabriel Genellina" <>
    > wrote:
    >
    >
    >
    > > En Sat, 28 Apr 2007 15:48:11 -0300,
    > > <> escribió:

    >
    > > > 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, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan Christensen
    Replies:
    4
    Views:
    605
    Peter Otten
    Jul 14, 2004
  2. Replies:
    2
    Views:
    280
  3. Dongsheng Ruan
    Replies:
    1
    Views:
    445
    Gabriel Genellina
    Jan 19, 2007
  4. ssecorp
    Replies:
    5
    Views:
    323
    Steven D'Aprano
    Aug 4, 2008
  5. Matthew Wilson
    Replies:
    0
    Views:
    253
    Matthew Wilson
    May 12, 2010
Loading...

Share This Page