RE: timeit

Discussion in 'Python' started by Robert Brewer, Jan 31, 2004.

  1. Duncan Smith wrote:
    > >>> 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


    I believe you need to put *any* initialization code (like "import
    subtract") into the "setup" parameter to Timer(). So try something like:

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

    SDC_table.RandomTable(1, 12, (2,3,4))")
    > >>> tim.timeit(10)



    HTH

    Robert Brewer
    MIS
    Amor Ministries
    Robert Brewer, Jan 31, 2004
    #1
    1. Advertising

  2. Robert Brewer

    Duncan Smith Guest

    "Robert Brewer" <> wrote in message
    news:...
    Duncan Smith wrote:
    > >>> 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


    I believe you need to put *any* initialization code (like "import
    subtract") into the "setup" parameter to Timer(). So try something like:

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

    SDC_table.RandomTable(1, 12, (2,3,4))")
    > >>> tim.timeit(10)


    That does the job. Cheers.

    Duncan
    Duncan Smith, Jan 31, 2004
    #2
    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:
    496
    Chad Netzer
    Aug 28, 2003
  2. Dennis Benzinger

    timeit.repeat() inaccuracy on W2K?

    Dennis Benzinger, Nov 25, 2003, in forum: Python
    Replies:
    1
    Views:
    330
    Raymond Hettinger
    Nov 26, 2003
  3. Duncan Smith

    timeit

    Duncan Smith, Jan 31, 2004, in forum: Python
    Replies:
    3
    Views:
    463
    Skip Montanaro
    Jan 31, 2004
  4. C. Barnes

    Simplified timeit

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

Share This Page