timeit

Duncan Smith buzzard at urubu.freeserve.co.uk
Sat Jan 31 10:49:47 EST 2004


"Robert Brewer" <fumanchu at amor.org> wrote in message
news:mailman.1071.1075531877.12720.python-list at python.org...
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





More information about the Python-list mailing list