python向量化与for循环耗时对比分析
更新时间:2022年05月19日 14:06:37 作者:Nani_xiao
这篇文章主要介绍了python向量化与for循环耗时对比分析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
向量化与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倍
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
python使用正则表达式去除中文文本多余空格,保留英文之间空格方法详解
这篇文章主要介绍了python使用正则表达式去除中文文本多余空格,保留英文之间空格方法详解,需要的朋友可以参考下2020-02-02OpenCV每日函数之BarcodeDetector类条码检测器
OpenCV在V4.5.3版本的contrib包中提供了一个barcode::BarcodeDetector类,用于条形码的识别,这篇文章主要介绍了OpenCV每日函数 BarcodeDetector条码检测器,需要的朋友可以参考下2022-06-06
最新评论