在Python中显示计算时间通常有两种方法:使用`time`模块和`datetime`模块。以下是使用这两种模块显示计算时间的示例:
使用`time`模块
```python
import time
获取当前时间戳
start_time = time.time()
执行一些计算或操作
...
获取操作后的时间戳
end_time = time.time()
计算并打印执行时间
elapsed_time = end_time - start_time
print(f"执行时间:{elapsed_time}秒")
使用`datetime`模块
```python
from datetime import datetime
获取当前时间
start_time = datetime.now()
执行一些计算或操作
...
获取操作后的时间
end_time = datetime.now()
计算并打印执行时间
elapsed_time = end_time - start_time
print(f"执行时间:{elapsed_time}")
以上代码展示了如何使用`time`模块和`datetime`模块来计算并显示Python程序的执行时间。您可以根据需要选择使用适合您需求的方法