在Python中,打印代码运行时间可以通过以下几种方法实现:
1. 使用`time`模块:
import timedef some_function():start_time = time.time()这里是你的代码end_time = time.time()execution_time = end_time - start_timeprint(f"函数执行时间:{execution_time}秒")
2. 使用`datetime`模块:
from datetime import datetimedef some_function():start_time = datetime.now()这里是你的代码end_time = datetime.now()execution_time = end_time - start_timeprint(f"函数执行时间:{execution_time}")
3. 使用第三方库`potime`(需要先安装):
pip install potime@potimedef some_function():这里是你的代码
当使用`potime`装饰器时,函数执行完毕后,`potime`会自动打印出该函数的运行时间。
请选择适合你需求的方法来计算和打印Python代码的运行时间

