Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: timeit

Reply
Thread Tools

RE: timeit

 
 
Robert Brewer
Guest
Posts: n/a
 
      01-31-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Duncan Smith
Guest
Posts: n/a
 
      01-31-2004

"Robert Brewer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
timeit.timeit and timeit.repeat give different answers Dan Christensen Python 4 07-14-2004 06:54 AM
Simplified timeit C. Barnes Python 0 06-08-2004 04:28 AM
timeit Duncan Smith Python 3 01-31-2004 04:00 PM
timeit.repeat() inaccuracy on W2K? Dennis Benzinger Python 1 11-26-2003 06:13 AM
timing a function with timeit? (newbie problem) news.west.cox.net Python 1 08-28-2003 01:24 AM



Advertisments