timeit

Discussion in 'Python' started by Duncan Smith, Jan 31, 2004.

  1. Duncan Smith

    Duncan Smith Guest

    This is possibly a stupid question, but I need to get this working quickly,
    and I guess I'm just too tired / pressured to see what I'm doing wrong.
    Clearly (from the output below) I can't pass the statement to an instance of
    timeit.Timer, but I can execute it using exec(). Any idea what I need to do
    to get this timed (using timeit). Cheers.

    >>> import SDC_table
    >>> import subtract
    >>> import timeit
    >>> t = SDC_table.RandomTable(1, 12, (2,3,4))
    >>> s = """\

    p = subtract.p_risk(t.values, 10)
    """
    >>> tim = timeit.Timer(stmt=s)
    >>> tim.timeit(10)


    Traceback (most recent call last):
    File "<pyshell#119>", line 1, in -toplevel-
    tim.timeit(10)
    File "C:\Python23\lib\timeit.py", line 158, in timeit
    return self.inner(it, self.timer)
    File "<timeit-src>", line 6, in inner
    NameError: global name 'subtract' is not defined

    # yet
    >>> exec(s)
    >>> p

    0.242621453769059
    Duncan Smith, Jan 31, 2004
    #1
    1. Advertising

  2. Duncan Smith

    Rich Krauter Guest

    Did you try enclosing arg to Timer in quotes? It is enclosed in quotes
    in the python tutorial:
    http://www.python.org/doc/current/tut/node12.html#SECTION00121000000000000000000

    Rich

    On Fri, 2004-01-30 at 23:21, Duncan Smith wrote:
    > This is possibly a stupid question, but I need to get this working quickly,
    > and I guess I'm just too tired / pressured to see what I'm doing wrong.
    > Clearly (from the output below) I can't pass the statement to an instance of
    > timeit.Timer, but I can execute it using exec(). Any idea what I need to do
    > to get this timed (using timeit). Cheers.
    >
    > >>> import SDC_table
    > >>> import subtract
    > >>> import timeit
    > >>> t = SDC_table.RandomTable(1, 12, (2,3,4))
    > >>> s = """\

    > p = subtract.p_risk(t.values, 10)
    > """
    > >>> tim = timeit.Timer(stmt=s)
    > >>> tim.timeit(10)

    >
    > Traceback (most recent call last):
    > File "<pyshell#119>", line 1, in -toplevel-
    > tim.timeit(10)
    > File "C:\Python23\lib\timeit.py", line 158, in timeit
    > return self.inner(it, self.timer)
    > File "<timeit-src>", line 6, in inner
    > NameError: global name 'subtract' is not defined
    >
    > # yet
    > >>> exec(s)
    > >>> p

    > 0.242621453769059
    >
    Rich Krauter, Jan 31, 2004
    #2
    1. Advertising

  3. Duncan> This is possibly a stupid question, but I need to get this
    Duncan> working quickly, and I guess I'm just too tired / pressured to
    Duncan> see what I'm doing wrong.

    Import the subtract module in a setup arg to your Timer() constructor:

    tim = timeit.Timer(setup-'import subtract', stmt=s)

    Skip
    Skip Montanaro, Jan 31, 2004
    #3
  4. >>>>> "Skip" == Skip Montanaro <> writes:


    Skip> tim = timeit.Timer(setup-'import subtract', stmt=s)

    Whoops! '-' should of course be '='...

    S
    Skip Montanaro, Jan 31, 2004
    #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. news.west.cox.net

    timing a function with timeit? (newbie problem)

    news.west.cox.net, Aug 28, 2003, in forum: Python
    Replies:
    1
    Views:
    498
    Chad Netzer
    Aug 28, 2003
  2. Dennis Benzinger

    timeit.repeat() inaccuracy on W2K?

    Dennis Benzinger, Nov 25, 2003, in forum: Python
    Replies:
    1
    Views:
    334
    Raymond Hettinger
    Nov 26, 2003
  3. Robert Brewer

    RE: timeit

    Robert Brewer, Jan 31, 2004, in forum: Python
    Replies:
    1
    Views:
    391
    Duncan Smith
    Jan 31, 2004
  4. C. Barnes

    Simplified timeit

    C. Barnes, Jun 8, 2004, in forum: Python
    Replies:
    0
    Views:
    292
    C. Barnes
    Jun 8, 2004
  5. Dan Christensen
    Replies:
    4
    Views:
    564
    Peter Otten
    Jul 14, 2004
Loading...

Share This Page