is there a way to simplify a 3-nested loop over a 3d-field? The code look like:
from itertools import product
for kx, ky, kz in product(freq, freq, freq):
k = np.sqrt(kx**2+ky**2+kz**2)
if int(k+0.5) < N/2.0:
yaxes[field][int(k+0.5)] += A[kx][ky][kz]
the shape of A is (N,N,N) and freq is a special iteration with the length N.
Maybe there is a numpy-tool to perform this code, cause this needs to long.
You can reduce time consumption by 20% with this simple optimization:
Use dis module to check byte code and timeit to measure speed of your algorithm. Below examples on how your code has changed.
Every time you write int(k+0.5) you get following byte code:
It’s better to calculate it once, so the next call can be much faster:
Same story for N/2.0. Instead of having this in every iteration:
you can just use pre-calculated NN: