import numpy as np import mytimer @mytimer.timer def dot_function(x,y): diff = x - y return diff.T.dot(diff) @mytimer.timer def npdot_function(x,y): diff = x - y return np.dot(diff.T,diff) @mytimer.timer def sqsum_function(x,y): diff = x - y return np.square(diff).sum() if __name__=='__main__': arr_res = [] mat_res = [] funs = [sqsum_function,dot_function, npdot_function] labs = ['np.square(diff).sum()','diff.T.dot(diff)','np.dot(diff.T,diff)'] cols = ['r','b','y'] for i in range(25): N = 2**i x = np.random.random((N,1)) y = np.random.random((N,1)) xx = np.asmatrix(x) yy = np.asmatrix(y) for f, lab, col in zip(funs,labs,cols): r,t1 = f(x,y) r,t2 = f(xx,yy) lab = lab if i==0 else '' # ternary, label only for first value in the data set arr_res.append([N,t1,lab,col]) mat_res.append([N,t2,lab,col]) import matplotlib.pyplot as plt plt.switch_backend('TkAgg') for r in arr_res: N,t,lab,col = r plt.loglog(N,t,'o',color=col, label=lab) for r in mat_res: N,t,lab,col = r plt.loglog(N,t,'s',color=col, label=lab) plt.title('Dot product using array or matrix') plt.xlabel('N') plt.ylabel('time (s)') plt.legend() plt.show()