Hi everyone, Operations such as np.sqrt or np.cos on numpy scalars are much slower than operations on an array of size 1 (benchmark below). The reason seems to be that a numpy scalar (e.g. np.float64(1.1)) is converted internally to an array, an array is created for the result of the operation and the result is converted back to a scalar (so three object constructions in total). Operating directly on an array creates an array for the result, which is returned (so one object construction in total).
Has anyone looked into improving this performance before or has ideas how to tackle this? With kind regards, Pieter Eendebak Example benchmark: import numpy as np from numpy import sqrt import time v=np.array([1.1]) t0=time.perf_counter() x=np.float64(v) for kk in range(1_000_000): w1=sqrt(x) dt1=time.perf_counter()-t0 print(dt1) t0=time.perf_counter() x=v for kk in range(1_000_000): w2=sqrt(x) dt2=time.perf_counter()-t0 print(dt2) print(f'factor {dt1/dt2:.2f} faster') assert(float(w1)==float(w2)) Results in 1.0878291579974757 0.5115369699997245 factor 2.13 faster
_______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-le...@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: arch...@mail-archive.com