網站首頁 編程語言 正文
前言
因為經常一訓練就是很多次迭代,所以找到效率比較高的操作能大大縮短運行時間,但這方面資料不足,所以自己記錄總結一下,有需要再補充
索引效率與內存占用比較
有時候我需要一個數組,然后可能會頻繁從中索引數據,那么我選擇list還是numpy array呢,這里做了一個簡單的實驗進行比較,環境python 3.6
import random
import numpy as np
import time
import sys
# import matplotlib
# matplotlib.use('agg')
import matplotlib.pyplot as plt
from collections import deque
start = time.time()
length = []
list_size = []
array_size = []
deque_size = []
list_time = []
array_time = []
deque_time = []
for l in range(5, 15000, 5):
print(l)
length.append(l)
a = [1] * l
b = np.array(a)
c = deque(maxlen=l)
for i in range(l):
c.append(1)
# print('list的size為:{}'.format(sys.getsizeof(a)))
# print('array的size為:{}'.format(sys.getsizeof(b)))
# print('deque的size為:{}'.format(sys.getsizeof(c)))
list_size.append(sys.getsizeof(a))
array_size.append(sys.getsizeof(b))
deque_size.append(sys.getsizeof(c))
for i in range(3):
if i == 0:
tmp = a
name = 'list'
elif i == 1:
tmp = b
name = 'array'
else:
tmp = c
name = 'deque'
s = time.time()
for j in range(1000000):
x = tmp[random.randint(0, len(a)-1)]
duration = time.time() - s
if name == 'list':
list_time.append(duration)
elif name == 'array':
array_time.append(duration)
else:
deque_time.append(duration)
duration = time.time() - start
time_list = [0, 0, 0]
time_list[0] = duration // 3600
time_list[1] = (duration % 3600) // 60
time_list[2] = round(duration % 60, 2)
print('用時:' + str(time_list[0]) + ' 時 ' + str(time_list[1]) + '分' + str(time_list[2]) + '秒')
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.plot(length, list_size, label='list')
ax1.plot(length, array_size, label='array')
ax1.plot(length, deque_size, label='deque')
plt.xlabel('length')
plt.ylabel('size')
plt.legend()
ax2 = fig.add_subplot(212)
ax2.plot(length, list_time, label='list')
ax2.plot(length, array_time, label='array')
ax2.plot(length, deque_time, label='deque')
plt.xlabel('length')
plt.ylabel('time')
plt.legend()
plt.show()
對不同大小的list,numpy array和deque進行一百萬次的索引,結果為
可以看出,numpy array對內存的優化很好,長度越大,其相比list和deque占用內存越少。
list比deque稍微好一點。因此如果對內存占用敏感,選擇優先級:numpy array>>list>deque。
時間上,在15000以下這個長度,list基本都最快。其中
- 長度<1000左右時,deque跟list差不多,選擇優先級:list≈ \approx≈deque>numpy array;
- 長度<9000左右,選擇優先級:list>deque>numpy array;
- 長度>9000左右,選擇優先級:list>numpy array>deque;
不過時間上的差距都不大,幾乎可以忽略,差距主要體現在內存占用上。因此如果對內存不敏感,list是最好選擇。
整個實驗使用i7-9700,耗時2.0 時 36.0分20.27秒,如果有人愿意嘗試更大的量級,更小的間隔,歡迎告知我結果。
添加效率比較
numpy的數組沒有動態改變大小的功能,因此這里numpy數據只是對其進行賦值。
import numpy as np
import time
from collections import deque
l = 10000000
a = []
b = np.zeros(l)
c = deque(maxlen=l)
for i in range(3):
if i == 0:
tmp = a
name = 'list'
elif i == 1:
tmp = b
name = 'array'
else:
tmp = c
name = 'deque'
start = time.time()
if name == 'array':
for j in range(l):
tmp[j] = 1
else:
for j in range(l):
tmp.append(1)
duration = time.time() - start
time_list = [0, 0, 0]
time_list[0] = duration // 3600
time_list[1] = (duration % 3600) // 60
time_list[2] = round(duration % 60, 2)
print(name + '用時:' + str(time_list[0]) + ' 時 ' + str(time_list[1]) + '分' + str(time_list[2]) + '秒')
結果為:
list用時:0.0 時 0.0分1.0秒
array用時:0.0 時 0.0分1.14秒
deque用時:0.0 時 0.0分0.99秒
可以看出,只有在非常大的量級上才會出現區別,numpy array的賦值是最慢的,list和deque差不多。
但平時這些差距幾乎可以忽略。
總結
原文鏈接:https://blog.csdn.net/qq_38163755/article/details/109392728
相關推薦
- 2022-08-17 yolov5中anchors設置實例詳解_python
- 2022-04-04 elementui組件select選擇不中的問題(組件select選擇器無法顯示選中的內容)
- 2022-09-20 Python使用Flask?Migrate模塊遷移數據庫_python
- 2022-06-01 python畫立方體--魔方_python
- 2022-04-22 Error:Module “./antd/es/badge/style“ does not exis
- 2022-06-25 Docker核心組件之聯合文件系統詳解_docker
- 2021-12-03 Go并發編程中sync/errGroup的使用_Golang
- 2022-06-26 Android界面一鍵變灰開發深色適配模式編程示例_Android
- 最近更新
-
- 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同步修改后的遠程分支