python - Avoid creating new arrays as results for numpy/scipy operations? -
for doing repeated operations in numpy/scipy, there's lot of overhead because operation return new object.
for example
for in range(100): x = a*x i avoid passing reference operation, in c
for in range(100): np.dot(a,x,x_new) #x_new store result of multiplication x,x_new = x_new,x is there way this? not mutiplication operations return matrix or vector.
see learning avoid unnecessary array copies in ipython books. there, note e.g. these guidelines:
a *= b will not produce copy, whereas:
a = * b will produce copy. also, flatten() copy, while ravel() copies if necessary , returns view otherwise (and should in general preferred). reshape() not produce copy, returns view.
furthermore, @hpaulj , @ali_m noted in comments, many numpy functions support out parameter, have @ docs. numpy.dot() docs:
out : ndarray, optional output argument.
this must have exact kind returned if not used. in particular, must have right type, must c-contiguous, , dtype must dtype returned dot(a,b). performance feature. therefore, if these conditions not met, exception raised, instead of attempting flexible.
Comments
Post a Comment