<p>使用此项计算时间:</p>
<pre><code>import time
time_start = time.clock()
#run your code
time_elapsed = (time.clock() - time_start)
</code></pre>
<p>如Python文档所引用:</p>
<blockquote>
<p><strong>time.clock()</strong></p>
<p>On Unix, return the current processor time as a floating
point number expressed in seconds. The precision, and in fact the very
definition of the meaning of “processor time”, depends on that of the
C function of the same name, but in any case, this is the function to
use for benchmarking Python or timing algorithms.</p>
<p>On Windows, this function returns wall-clock seconds elapsed since the
first call to this function, as a floating point number, based on the
Win32 function QueryPerformanceCounter(). The resolution is typically
better than one microsecond.</p>
</blockquote>
<p><strong>引用</strong>:<a href="http://docs.python.org/library/time.html" rel="noreferrer">http://docs.python.org/library/time.html</a></p>
<hr/>
<p>使用此项计算内存:</p>
<pre><code>import resource
resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
</code></pre>
<p><strong>引用</strong>:<a href="http://docs.python.org/library/resource.html" rel="noreferrer">http://docs.python.org/library/resource.html</a></p>