from random import randint import numpy as np def dist1(loc, other_loc): return (loc[0]-other_loc[0])**2 + (loc[1]-other_loc[1])**2 + (loc[2]-other_loc[2])**2 def yield_pts(n): for i in range(0,n): p1 = (10000-randint(0,20000), 10000-randint(0,20000), 10000-randint(0,20000)) p2 = (10000-randint(0,20000), 10000-randint(0,20000), 10000-randint(0,20000)) yield p1, p2 def native(ptlst): return [dist1(p[0], p[1]) for p in ptlst] def fast(x1s, y1s, z1s, x2s, y2s, z2s): p1 = np.array([x1s, y1s, z1s]) p2 = np.array([x2s, y2s, z2s]) squared_dist = np.sum((p1-p2)**2, axis=0) return np.sqrt(squared_dist) import cProfile ptlst = [(p1, p2) for (p1, p2) in yield_pts(41000*5*10)] cProfile.runctx("native(ptlst)", globals(), locals()) n=41000*5*10 x1s = [10000-randint(0,20000) for i in range(0, n)] y1s = [10000-randint(0,20000) for i in range(0, n)] z1s = [10000-randint(0,20000) for i in range(0, n)] x2s = [10000-randint(0,20000) for i in range(0, n)] y2s = [10000-randint(0,20000) for i in range(0, n)] z2s = [10000-randint(0,20000) for i in range(0, n)] cProfile.runctx("fast(x1s, y1s, z1s, x2s, y2s, z2s)", globals(), locals())