Python 中
time.perf_counter()和time.time()的区别
很多人在写 Python 代码时,如果想统计一段代码运行了多久,第一反应通常是:
import time
start = time.time()
# 一段代码
end = time.time()
print(end - start)
这样写看起来没有问题,但在真实项目里,这其实并不是最推荐的做法。尤其是在下面这些场景中:
- 模型推理耗时统计
- FastAPI 接口耗时监控
- 算法性能测试
- Benchmark
- 高并发服务中的延迟统计
一般来说,我更推荐使用time.perf_counter()。那么,time.time() 和 time.perf_counter() 到底有什么区别?什么时候该用哪个?我们下面会进行详细分析。

一、time.time():获取当前真实时间
time.time() 返回的是当前时间戳。
import time
print(time.time())
输出类似:
1713156000.123456
它表示从 1970 年 1 月 1 日 00:00:00 到现在,经过了多少秒。
因此,time.time() 本质上是在告诉你:“现在几点了?”
1.1 典型用途
time.time() 更适合做:
-
记录日志时间
-
生成时间戳
-
判断某个时间是否超时
-
保存数据库中的时间字段
例如:
import time
log_time = time.time()
print(f"日志时间:{log_time}")
或者:
if time.time() - last_update > 60:
print("距离上次更新时间已经超过 1 分钟")
二、time.perf_counter():高精度性能计时器
time.perf_counter() 的目标和 time.time() 完全不同。
它不是用来告诉你“现在几点”,而是用来告诉你:“两段代码之间,到底过了多久?”
import time
start = time.perf_counter()
# 一段代码
end = time.perf_counter()
print(end - start)
输出可能是:
0.0002315
这表示代码运行了 0.23 毫秒。
2.1 为什么它更适合计时?
因为 time.perf_counter() 有两个关键特点。
(1)精度更高
它通常会使用系统中最精确的时钟。在不同系统上,精度可能达到微秒级、纳秒级。
例如,下面两段代码的差异:
import time
start = time.time()
for _ in range(1000):
pass
print(time.time() - start)
可能输出:
0.0
因为代码执行太快,time.time() 的精度不够。
但如果换成:
import time
start = time.perf_counter()
for _ in range(1000):
pass
print(time.perf_counter() - start)
你就能看到一个更准确的耗时,例如:
0.0000312
(2) 不会受到系统时间修改影响
time.time() 会读取系统时间。
如果你手动修改电脑时间、服务器自动同步 NTP 时间、容器中的系统时间发生变化,那么 time.time() 的结果可能会突然跳变。
例如:
start = time.time()
# 执行过程中系统时间被调慢了
end = time.time()
print(end - start)
甚至有可能输出负数:
-0.8
而 time.perf_counter() 是一个“单调递增”的时钟。它只会不断变大,不会倒退。因此它更适合做耗时统计。
2.2 两者最直观的区别
| 对比项 | time.time() | time.perf_counter() |
|---|---|---|
| 作用 | 获取当前真实时间 | 统计程序耗时 |
| 是否受系统时间影响 | 会 | 不会 |
| 是否单调递增 | 不保证 | 保证 |
| 精度 | 较低 | 更高 |
| 是否适合 Benchmark | 不推荐 | 推荐 |
| 常见用途 | 日志、时间戳 | 性能测试、接口耗时 |
三、真实项目里应该怎么选?
3.1 如果你想记录“什么时候发生”
那就使用time.time(),例如create_time = time.time(),适用于:
- 日志系统
- 数据库存储
- 文件名时间戳
3.2 如果你想记录某个过程“花了多久时间”
那就用time.perf_counter(),进行高精度计时。例如:
import time
start = time.perf_counter()
model.predict(data)
latency = time.perf_counter() - start
print(f"推理耗时: {latency:.6f}s")
这也是模型部署、FastAPI 服务、性能分析中最推荐的写法。
四、FastAPI 中统计接口耗时的正确写法
很多人在 FastAPI 中会这样写:
@app.get("/predict")
def predict():
start = time.time()
result = model.predict(x)
print(time.time() - start)
return result
更推荐:
import time
from fastapi import FastAPI
app = FastAPI()
@app.get("/predict")
def predict():
start = time.perf_counter()
result = model.predict(x)
cost = time.perf_counter() - start
print(f"接口耗时: {cost:.6f}s")
return result
这样即使服务器时间被同步、容器时间发生变化,耗时统计仍然准确。
五、进一步补充:还有一个 time.process_time()
除了这两个函数,Python 里还有一个:
time.process_time()
它统计的是CPU 真正执行代码的时间。因此不会统计:
-
sleep -
网络等待
-
数据库等待
-
I/O 等待
例如:
import time
start = time.process_time()
time.sleep(1)
print(time.process_time() - start)
输出可能接近0.0,因为 CPU 并没有真正工作。因此:
- 想看接口总耗时 → 用
perf_counter - 想分析 CPU 消耗 → 用
process_time
六、总结
# 获取当前时间
now = time.time()
# 统计代码耗时
start = time.perf_counter()
# do something
cost = time.perf_counter() - start
在真实工程里,尤其是算法部署、FastAPI、模型推理、接口压测等场景中:不要再用 time.time() 做性能统计,而应该优先使用 time.perf_counter()。因为它更准确、更稳定,也更符合工程实践。

187

被折叠的 条评论
为什么被折叠?



