【发布时间】:2021-07-17 02:50:47
【问题描述】:
我正在尝试使用timeit 比较我的函数的速度,但保存了这些结果,但我无法实现。让我向您展示一个玩具示例,尝试比较不同的矩阵乘法函数:
from timeit import timeit
import numpy as np
def prod_tensorial (A,B) : return np.tensordot(A,B,(-1,0))
def prod_punto (A,B) : return np.dot(A,B)
def prod_sumeinsten (A,B) : return np.einsum('ij,jk->ik',A,B)
A = np.random.rand(1000,2000)
B = np.random.rand(2000,3000)
fs = ( prod_tensorial, prod_punto, prod_sumeinsten )
ts = [ ]
ps = [ ]
for f in fs:
ts += [ timeit('ps += [f(A,B)]',number=1) ] # error: ps & f didn't recognized
#ts += [ timeit(lambda: f(A,B),number=1) ] # it works but lossing results
print( ts[-1] )
print(np.allclose(*ps))
你怎么看,我无法保存结果。你知道我该怎么做吗?
【问题讨论】:
标签: python-3.x timeit