-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove allocations in interpolants(grid::RectangleGrid, x::AbstractVector) and interpolate using view #42
Conversation
…ctor) and interpolate using view
Overall this change looks great to me. I'm surprised Could the indentation of the code be adjusted to match with the existing code? Usually that delta is a matter of tabs vs. spaces. |
@tawheeler I think the allocation was due to the argument that was being passed to the dot function (data[index]). I didn't realize the indentation mismatch before. Thank you for pointing it out. I have replaced tabs with spaces and it now matches the existing code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! There might be a better way to do the for loop, e.g. with sum
, but I think we should just merge this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a possible alternative for the for loop
v = 0.0 | ||
for (i,data_ind) in enumerate(index) | ||
v += data[data_ind]*weight[i] | ||
end | ||
return v |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this work?
v = 0.0 | |
for (i,data_ind) in enumerate(index) | |
v += data[data_ind]*weight[i] | |
end | |
return v | |
return dot(view(data, index), weight) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zsunberg Yes, this works too. It just slows down the computation by around 15 ns (checked multiple times). If that is acceptable, I can make this change. Let me know.
Before making the change suggested by Dr. Sunberg
After making the change suggested by Dr. Sunberg
We can also use the sum
function like you mentioned but it also causes a similar increase in total time.
sum( i->data[index[i]]*weight[i], 1:length(index))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either one is fine. dot
is easier to read, but the for loop performs slightly better.
Performance comparison
Before these modifications
After these modifications
If there are any issues with it, please let me know. Thanks!