点此进入CSDN

点此添加QQ好友 加载失败时会显示




numpy函数向量化,np.vectorize

 

import numpy as np
import time


def myfunc(a, b):
    if a>b:
        return a-b
    else:
        return a+b

vfunc = np.vectorize(myfunc)
t0=time.time()
vfunc(np.random.randint(5,size=10000000), 2)
t1=time.time()
print('speed_times:'+str(t1-t0))
#speed_times:3.2735631465911865
t0=time.time()
[myfunc(data, 2) for data in np.random.randint(5,size=10000000)]
t1=time.time()
print('speed_times:'+str(t1-t0))

 

结果:向量化后的并行运算速度要比普通的执行速度快很多

speed_times:2.7337491512298584
speed_times:9.00490427017212

 

posted @ 2021-03-12 17:29  高颜值的殺生丸  阅读(897)  评论(0编辑  收藏  举报

作者信息

昵称:

刘新宇

园龄:4年6个月


粉丝:1209


QQ:522414928