> For the complete documentation index, see [llms.txt](https://m0uk4.gitbook.io/notebooks/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://m0uk4.gitbook.io/notebooks/mouka/python/python-tricks/ji-suan-dai-ma-zhi-hang-shi-jian.md).

# 计算代码执行时间

{% hint style="info" %}
`timeit 模块可用来测量`**`少量代码`**`的执行时间`&#x20;
{% endhint %}

```python
>>> import timeit
>>> timeit.timeit('"-".join(str(n) for n in range(100))',
                  number=10000)
0.3412662749997253

>>> timeit.timeit('"-".join([str(n) for n in range(100)])',
                  number=10000)
0.2996307989997149

>>> timeit.timeit('"-".join(map(str, range(100)))',
                  number=10000)
0.24581470699922647
```
