python - Improve performance of function without parallelization -
A few weeks ago I posted a question () which found a very good answer. This question is related to that because it uses user-recommended performance improvements.
I need to improve the performance of such a function in which three factors are calculated and then apply exponentially to them. / P>
My code has a MWE
:
import as NMP timeline def defame_data (N): #Some random Generate data return np.random.uniform (0, 10., N) # Data list. Array1 = np.array ([random_data (4) _ in category (1000)]) array2 = np.array ([random_data (3) _ in category _ (_g)] # function.fffcfc (): # For the elem in array 1: # If one of these values is there, avoid numerical errors. E_1, e_2 = maximum (elem [0], 1e-10), Max ( Elem [1], 1e-10) # Get three parameters A = 1./(e_1*e_2) B = -0.5 * ((elem [2] -rhy 2 [, 0]) / e_1) ** 2 C = -0.5 * ((elem [3] -Rah 2 [:, 1]) / E2) ** 2 # Apply Exponance Price = A * NP.XP (B + C) # Store value in the preset. Lst.append (value) return lst # time function Func_time = timeit.timeit (func, number = 100) print func_time
What would be done without recurring parallelization Is speed of func
possible?
Even what I have so far My attitude is to make as much mathematics as possible as <>
- >
-
b
andc
Reactor calculation to split into factor, some of these can be counted within numpy Codedef optfunc (): e0 = array1 [, 0] e1 = array1 [, 1] e2 = array1 [ , 2] e3 = array1 [, 3] Ar0 = array2 [, 0] ar1 = array2 [, 1] as = 1 ./ (ee0 * e1) bfactors = -0.5 * (1 / e ** 2) CF Actors = -0.5 * (1 / E1 ** 2) for lst = [] i, AMM enumerate (array 1): B = ((AMM [2] - R) ** 2) * BF Actors [i] c = ((AMM [3] - R1 1) ** 2) * Cfactors [i] value = as [i] * np.exp (B + C) lst.append (value) return lst print Np.allclose (optfunc), func ()) # time function Func_time = timeit.timeit (func, number = 10) opt_func_time = timeit.timeit (optfunction, number = 10) print "% .3fs ->% .3fs "% (Func_time, opt_func_time)
Result:
True 0.759s - & gt; 0.485 s
This time I got trapped. I managed to do it completely without python for the loops, but it still does not seem to be that I am slower than the later version:
def optfunc (): X = array1 y = array2 x0 = x [,, 0] x1 = x [,, 1] x2 = x [,, 2] x3 = x [,, 3] y0 = y [:, 0] y1 = Y [:, 1] A = 1 ./(x0 * x1) BF Actors = -0.5 * (1 / x0 ** 2) factor = -0.5 * (1 / x1 ** 2) b = (N.P. [X2]) - y0) ** 2 * Np.transpose ([BFactors]) C = (N.P. managed ([x3]) y1) ** 2 * NP management ([Cfactors]) np.transpose return ( [A]) * NP.XP (B + C)
Parine G:
True 0.780s -> 0.558s
However note that you later get a
np.array
, whereas in the past you get only one python list ... this difference But I'm not sure.
Comments
Post a Comment