Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / lib / node465.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="lib.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="lib.html" title='Python Library Reference' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='index' href='genindex.html' title='Index' />
10<link rel='last' href='about.html' title='About this document...' />
11<link rel='help' href='about.html' title='About this document...' />
12<link rel="prev" href="node464.html" />
13<link rel="parent" href="module-timeit.html" />
14<link rel="next" href="internet.html" />
15<meta name='aesop' content='information' />
16<title>10.10.2 Examples</title>
17</head>
18<body>
19<DIV CLASS="navigation">
20<div id='top-navigation-panel' xml:id='top-navigation-panel'>
21<table align="center" width="100%" cellpadding="0" cellspacing="2">
22<tr>
23<td class='online-navigation'><a rel="prev" title="10.10.1 Command Line Interface"
24 href="node464.html"><img src='../icons/previous.png'
25 border='0' height='32' alt='Previous Page' width='32' /></A></td>
26<td class='online-navigation'><a rel="parent" title="10.10 timeit "
27 href="module-timeit.html"><img src='../icons/up.png'
28 border='0' height='32' alt='Up One Level' width='32' /></A></td>
29<td class='online-navigation'><a rel="next" title="11. Internet Protocols and"
30 href="internet.html"><img src='../icons/next.png'
31 border='0' height='32' alt='Next Page' width='32' /></A></td>
32<td align="center" width="100%">Python Library Reference</td>
33<td class='online-navigation'><a rel="contents" title="Table of Contents"
34 href="contents.html"><img src='../icons/contents.png'
35 border='0' height='32' alt='Contents' width='32' /></A></td>
36<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
37 border='0' height='32' alt='Module Index' width='32' /></a></td>
38<td class='online-navigation'><a rel="index" title="Index"
39 href="genindex.html"><img src='../icons/index.png'
40 border='0' height='32' alt='Index' width='32' /></A></td>
41</tr></table>
42<div class='online-navigation'>
43<b class="navlabel">Previous:</b>
44<a class="sectref" rel="prev" href="node464.html">10.10.1 Command Line Interface</A>
45<b class="navlabel">Up:</b>
46<a class="sectref" rel="parent" href="module-timeit.html">10.10 timeit </A>
47<b class="navlabel">Next:</b>
48<a class="sectref" rel="next" href="internet.html">11. Internet Protocols and</A>
49</div>
50<hr /></div>
51</DIV>
52<!--End of Navigation Panel-->
53
54<H2><A NAME="SECTION00121020000000000000000">
5510.10.2 Examples</A>
56</H2>
57
58<P>
59Here are two example sessions (one using the command line, one using
60the module interface) that compare the cost of using
61<tt class="function">hasattr()</tt> vs. <tt class="keyword">try</tt>/<tt class="keyword">except</tt> to test for
62missing and present object attributes.
63
64<P>
65<div class="verbatim"><pre>
66% timeit.py 'try:' ' str.__nonzero__' 'except AttributeError:' ' pass'
67100000 loops, best of 3: 15.7 usec per loop
68% timeit.py 'if hasattr(str, "__nonzero__"): pass'
69100000 loops, best of 3: 4.26 usec per loop
70% timeit.py 'try:' ' int.__nonzero__' 'except AttributeError:' ' pass'
711000000 loops, best of 3: 1.43 usec per loop
72% timeit.py 'if hasattr(int, "__nonzero__"): pass'
73100000 loops, best of 3: 2.23 usec per loop
74</pre></div>
75
76<P>
77<div class="verbatim"><pre>
78&gt;&gt;&gt; import timeit
79&gt;&gt;&gt; s = """\
80... try:
81... str.__nonzero__
82... except AttributeError:
83... pass
84... """
85&gt;&gt;&gt; t = timeit.Timer(stmt=s)
86&gt;&gt;&gt; print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
8717.09 usec/pass
88&gt;&gt;&gt; s = """\
89... if hasattr(str, '__nonzero__'): pass
90... """
91&gt;&gt;&gt; t = timeit.Timer(stmt=s)
92&gt;&gt;&gt; print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
934.85 usec/pass
94&gt;&gt;&gt; s = """\
95... try:
96... int.__nonzero__
97... except AttributeError:
98... pass
99... """
100&gt;&gt;&gt; t = timeit.Timer(stmt=s)
101&gt;&gt;&gt; print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
1021.97 usec/pass
103&gt;&gt;&gt; s = """\
104... if hasattr(int, '__nonzero__'): pass
105... """
106&gt;&gt;&gt; t = timeit.Timer(stmt=s)
107&gt;&gt;&gt; print "%.2f usec/pass" % (1000000 * t.timeit(number=100000)/100000)
1083.15 usec/pass
109</pre></div>
110
111<P>
112To give the <tt class="module">timeit</tt> module access to functions you
113define, you can pass a <code>setup</code> parameter which contains an import
114statement:
115
116<P>
117<div class="verbatim"><pre>
118def test():
119 "Stupid test function"
120 L = []
121 for i in range(100):
122 L.append(i)
123
124if __name__=='__main__':
125 from timeit import Timer
126 t = Timer("test()", "from __main__ import test")
127 print t.timeit()
128</pre></div>
129
130<P>
131
132<DIV CLASS="navigation">
133<div class='online-navigation'>
134<p></p><hr />
135<table align="center" width="100%" cellpadding="0" cellspacing="2">
136<tr>
137<td class='online-navigation'><a rel="prev" title="10.10.1 Command Line Interface"
138 href="node464.html"><img src='../icons/previous.png'
139 border='0' height='32' alt='Previous Page' width='32' /></A></td>
140<td class='online-navigation'><a rel="parent" title="10.10 timeit "
141 href="module-timeit.html"><img src='../icons/up.png'
142 border='0' height='32' alt='Up One Level' width='32' /></A></td>
143<td class='online-navigation'><a rel="next" title="11. Internet Protocols and"
144 href="internet.html"><img src='../icons/next.png'
145 border='0' height='32' alt='Next Page' width='32' /></A></td>
146<td align="center" width="100%">Python Library Reference</td>
147<td class='online-navigation'><a rel="contents" title="Table of Contents"
148 href="contents.html"><img src='../icons/contents.png'
149 border='0' height='32' alt='Contents' width='32' /></A></td>
150<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
151 border='0' height='32' alt='Module Index' width='32' /></a></td>
152<td class='online-navigation'><a rel="index" title="Index"
153 href="genindex.html"><img src='../icons/index.png'
154 border='0' height='32' alt='Index' width='32' /></A></td>
155</tr></table>
156<div class='online-navigation'>
157<b class="navlabel">Previous:</b>
158<a class="sectref" rel="prev" href="node464.html">10.10.1 Command Line Interface</A>
159<b class="navlabel">Up:</b>
160<a class="sectref" rel="parent" href="module-timeit.html">10.10 timeit </A>
161<b class="navlabel">Next:</b>
162<a class="sectref" rel="next" href="internet.html">11. Internet Protocols and</A>
163</div>
164</div>
165<hr />
166<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
167</DIV>
168<!--End of Navigation Panel-->
169<ADDRESS>
170See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
171</ADDRESS>
172</BODY>
173</HTML>