網站首頁 編程語言 正文
一、需求說明
在Python程序的開發過程中,一些程序需要獲取函數或程序的開始時間、結束時間和時間間隔等內容用來分析和處理內容
二、需求分析
涉及到函數或程序的運行時間,那么必然需要用到時間模塊,只用知道時間模塊中對應的時間信息獲取即可進響應的時間計算。
三、實現方法
3.1、獲取執行時間方式一
①導入time模塊
#導入time模塊
import time
②獲取開始時間、結束時間、時間間隔
#1-獲取開始時間
startTime=time.time()
#需要執行的函數或程序
#2-獲取結束時間
endtime=time.time()
#3-獲取時間間隔
diffrentTime=endtime-startTime
③示例
import time,random
def CreateDecimalPerThirtySecods(delaySeconds=3):
time.sleep(delaySeconds)
decimal= random.random()
print(decimal)
#測試時間間隔方式一
startTime=time.time()
print('time.time()開始打印隨機小數,開始時間是:',startTime)
time.sleep(3)
endtime=time.time()
print('time.time()3秒后的時間是:',endtime)
diffrentTime=endtime-startTime
print('time.time()相差的時間是:',diffrentTime,' 秒')
示例執行結果如下:
3.2、獲取執行時間方式二
①導入datetime模塊
#導入datetime模塊
import datetime
②獲取開始時間、結束時間、時間間隔
#1-獲取開始時間
startTime=datetime.datetime.now()
#需要執行的函數或程序
#2-獲取結束時間
endtime=datetime.datetime.now()
#3-獲取時間間隔
diffrentTime=(endtime-startTime).seconds
③示例
import datetime,time,random
def CreateDecimalPerThirtySecods(delaySeconds=3):
time.sleep(delaySeconds)
decimal= random.random()
print(decimal)
#測試時間間隔方式二
print('')
startTime=datetime.datetime.now()
print('datetime.datetime.now()開始打印隨機小數,開始時間是:',startTime)
time.sleep(3)
endtime=datetime.datetime.now()
print('datetime.datetime.now()3秒后的時間是:',endtime)
diffrentTime=(endtime-startTime).seconds
print('datetime.datetime.now()相差的時間是:',diffrentTime,' 秒')
示例執行結果如下:
原文鏈接:https://blog.csdn.net/xiaochenXIHUA/article/details/128969804
相關推薦
- 2023-04-06 C++內存對齊的實現_C 語言
- 2022-04-26 Entity?Framework?Core延遲加載(懶加載)用法_實用技巧
- 2022-04-19 python 讀寫yaml
- 2022-06-23 分布式醫療掛號系統整合Gateway網關解決跨域問題_其它綜合
- 2023-01-31 go中make用法及常見的一些坑_Golang
- 2022-05-08 繼docker之后podman容器技術崛起_docker
- 2022-03-13 .NET中IoC框架Autofac用法講解_自學過程
- 2022-06-01 Python基于Pytorch的特征圖提取實例_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支