Namespaces and the timeit module

Discussion in 'Python' started by Roy Smith, Dec 14, 2004.

  1. Roy Smith

    Roy Smith Guest

    I'm playing with the timeit module, and can't figure out how to time a
    function call. I tried:

    def foo ():
    x = 4
    return x

    t = timeit.Timer ("foo()")
    print t.timeit()

    and quickly figured out that the environment the timed code runs under
    is not what I expected:

    Traceback (most recent call last):
    File "./d.py", line 10, in ?
    print t.timeit()
    File "/usr/local/lib/python2.3/timeit.py", line 158, in timeit
    return self.inner(it, self.timer)
    File "<timeit-src>", line 6, in inner
    NameError: global name 'foo' is not defined

    In fact, trying to time "print dir()" gets you:

    ['_i', '_it', '_t0', '_timer']

    It seems kind of surprising that I can't time functions. Am I just not
    seeing something obvious?
     
    Roy Smith, Dec 14, 2004
    #1
    1. Advertising

  2. Roy Smith <> writes:

    > I'm playing with the timeit module, and can't figure out how to time a
    > function call. I tried:
    >
    > def foo ():
    > x = 4
    > return x
    >
    > t = timeit.Timer ("foo()")
    > print t.timeit()
    >
    > and quickly figured out that the environment the timed code runs under
    > is not what I expected:
    >
    > Traceback (most recent call last):
    > File "./d.py", line 10, in ?
    > print t.timeit()
    > File "/usr/local/lib/python2.3/timeit.py", line 158, in timeit
    > return self.inner(it, self.timer)
    > File "<timeit-src>", line 6, in inner
    > NameError: global name 'foo' is not defined
    >
    > In fact, trying to time "print dir()" gets you:
    >
    > ['_i', '_it', '_t0', '_timer']
    >
    > It seems kind of surprising that I can't time functions. Am I just not
    > seeing something obvious?


    Like the documentation for Timer? :)

    class Timer([stmt='pass' [, setup='pass' [, timer=<timer function>]]])

    You can't use statements defined elsewhere, you have to define them in
    the setup arguments (as a string). Like this:


    define_foo = '''
    def foo():
    x = 4
    return x
    '''

    t = timeit.Timer("foo()" setup=define_foo)
    print t.timeit()


    One common idiom I've seen is to put your definition of foo() in a
    module (say x.py), then, from the command line:

    $ python -m timeit -s 'from x import foo' 'foo()'

    (the -m is for python 2.4 to run the timeit module; use the full path
    to timeit.py instead for earlier pythons)

    Alternatively, the examples for the timeit module has another way to
    time functions defined in a module.

    --
    |>|\/|<
    /--------------------------------------------------------------------------\
    |David M. Cooke
    |cookedm(at)physics(dot)mcmaster(dot)ca
     
    David M. Cooke, Dec 14, 2004
    #2
    1. Advertising

  3. Roy Smith

    Roy Smith Guest

    (David M. Cooke) wrote:

    > > It seems kind of surprising that I can't time functions. Am I just not
    > > seeing something obvious?

    >
    > Like the documentation for Timer? :)
    >
    > class Timer([stmt='pass' [, setup='pass' [, timer=<timer function>]]])
    >
    > You can't use statements defined elsewhere, you have to define them in
    > the setup arguments (as a string).


    Doh! Of course. Now that you point it out, it makes perfect sense, but
    I didn't get that from reading the description. Thanks.
     
    Roy Smith, Dec 14, 2004
    #3
    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:
    569
    Peter Otten
    Jul 14, 2004
  2. fortepianissimo

    module timeit and variable scope

    fortepianissimo, Aug 2, 2004, in forum: Python
    Replies:
    0
    Views:
    270
    fortepianissimo
    Aug 2, 2004
  3. Michele Simionato

    Namespaces and the timeit module

    Michele Simionato, Dec 14, 2004, in forum: Python
    Replies:
    0
    Views:
    270
    Michele Simionato
    Dec 14, 2004
  4. Replies:
    2
    Views:
    271
  5. ChaosKCW
    Replies:
    5
    Views:
    295
    bruno at modulix
    Nov 11, 2005
Loading...

Share This Page