網站首頁 編程語言 正文
向量化與for循環耗時對比
深度學習中,可采用向量化替代for循環,優化耗時問題
對比例程如下,參考Andrew NG的課程筆記
import time
import numpy as np
a = np.random.rand(1000000)
b = np.random.rand(1000000)
tic = time.time()
c = np.dot(a,b)
toc = time.time()
print(c)
print("Vectorized version: " , str(1000*(toc-tic)) + "ms")
c = 0
tic1 = time.time()
for i in range(1000000):
c += a[i]*b[i]
toc1 = time.time()
print(c)
print("For loop version: " , str(1000*(toc1-tic1)) + "ms")
處理百萬數據,耗時相差400多倍。
效果圖:
向量化數據的相比于for循環的優勢
例子
import numpy as np
import time
a = np.random.rand(1000000)
b = np.random.rand(1000000)
tic = time.time()
c = np.dot(a,b)
toc = time.time()
print?
print(“vectorized version:” + str((toc-tic))+“s”)
c1 = 0
tic = time.time()
for i in range(1000000):
c1 += a[i]*b[i]
toc = time.time()
print(c1)
print(“Nonvectorized version:” + str(toc-tic)+“s”)
結果
250487.97870397285
vectorized version:0.002000093460083008s
250487.9787039739
Nonvectorized version:0.957054615020752s
可以看出向量化后執行時間比使用for循環快478倍
原文鏈接:https://blog.csdn.net/xiao_lxl/article/details/78134537
相關推薦
- 2022-12-06 Android?Doze模式下Alarm定時任務實現流程詳解_Android
- 2022-12-06 C++如何將字符串顛倒輸出_C 語言
- 2022-11-25 詳解React中Fragment的簡單使用_React
- 2022-06-17 Go模板template用法詳解_Golang
- 2022-12-11 C語言計算分段函數問題_C 語言
- 2022-08-23 Selenium多窗口切換解決方案_python
- 2022-12-07 React?Context?變遷及背后實現原理詳解_React
- 2023-04-09 使用Pytest.main()運行時參數不生效問題解決_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同步修改后的遠程分支