擅长:python、mysql、java
<p>我发现自己一次又一次地解决这个问题,所以我最终为它创建了一个<a href="https://github.com/mherrmann/timer-cm" rel="noreferrer" title="library">library</a>。用<code>pip install timer_cm</code>安装。然后:</p>
<pre><code>from time import sleep
from timer_cm import Timer
with Timer('Long task') as timer:
with timer.child('First step'):
sleep(1)
for _ in range(5):
with timer.child('Baby steps'):
sleep(.5)
</code></pre>
<p>输出:</p>
<pre><code>Long task: 3.520s
Baby steps: 2.518s (71%)
First step: 1.001s (28%)
</code></pre>