I am trying to speed up my code. The biggest problem is a few nested loops I have (they have to iterate over 25000 cells). However, when I try to get rid of these nested loops, I get a different result and I don't seem to get why.
This is one of the nested loop:
for i in range(N):
for j in range(N):
# value added in sector i (month k+1)
VA[i,k+1]= VA[i,k+1] - IO[j,i]*(Produc[i,k+1]/Produc[i,0])
This is what I did to get rid of the inner loop:
for in range(N):
VA[i,k+1]=VA[i,k+1] - np.sum(IO[:,i])*(Produc[i,k+1]/Produc[i,0])
Thank you for very much your help.
The problem is that assigning to VA constricts the type to VA.dtype, so you can lose accuracy if VA.dtype is less precise than the result from
VA[i,k+1] - IO[j,i]*(Produc[i,k+1]/Produc[i,0]).To keep this rounding you'd want:
...assuming you're not more happy with the more accurate version!
Some more painstaking research has shown that if the subtractions take the data through 0, the behaviour isn't perfectly emulated. I wouldn't bother though, because emulating subtle bugs is a waste of time ;).
Note that if you're happy with
you can also do
which I think is equivalent.
Note that this assumes that
Nis the perfect fit for a lot of these. It could be thatVA[:N, :N]is a subset of VA, in which case that's the problem and you should crop everything toNwithin the calculations.