lolol this is incredibly slow, but I noticed a pattern and was able to easy escape based on it. I think using numpy is the way to go in order to speed this up.
I'm building Commonlounge.com, an educational platform with wiki-based short courses on Machine Learning, Web Development, UX/UI Design and many more topics!
We need to add that 4th last term sumGrid[x+size][y+size] since we have subtracted it twice as part of terms 2 and 3. The solution now avoids two inner for loops — making it much faster :)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
lolol this is incredibly slow, but I noticed a pattern and was able to easy escape based on it. I think using numpy is the way to go in order to speed this up.
what was the pattern?
It increased to the high point then decreased from there! After it went negative it stayed negative.
And it kept going negative even after you switched block sizes? Huh, cool.
yup! positive up to a point, then all negative!
A speed up I can suggest: pre-compute another grid, say
sumGrid
wheresumGrid[x][y] = sum(grid[i][j]
wherei
is inrange(x, N)
andj
inrange(y, N)
So, each element in this new
sumGrid
is the sum of all elements below and right to it, including itself.Then
sum(x,y,size)
is now just:As an example,
sum(3,2,5)
would look like this:We need to add that 4th last term
sumGrid[x+size][y+size]
since we have subtracted it twice as part of terms 2 and 3. The solution now avoids two innerfor
loops — making it much faster :)