網站首頁 編程語言 正文
一、需求說明
在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-16 c#?成員類型訪問權限低于字段本身的實現_C#教程
- 2021-12-06 c#二叉樹存儲介紹_C#教程
- 2023-01-28 Python進程間通訊與進程池超詳細講解_python
- 2022-12-02 C語言實現三子棋小游戲的示例代碼_C 語言
- 2022-05-29 C#中使用HttpPost調用WebService的方法_C#教程
- 2022-08-04 python連接FTP服務器的實現方法_python
- 2022-07-12 CSS樣式:彈性容器上的樣式
- 2022-03-19 centos7修改網卡后無法上網問題解決過程_Linux
- 最近更新
-
- 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同步修改后的遠程分支