Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
While profiling an application that uses ImgLib2, I learned that multiple threads concurrently summing up large arrays using
net.imglib2.util.RealSum
are responsible for a big chunk of CPU-time.Proposed changes
Subsequently, I researched numerically stable ways of summing up floating point numbers, and learned that the current implementation could be improved by using a variant of the Kahan summation algorithm.
I implemented this algorithm in
RealSum
, keeping the API stable. Since there is no capacity anymore, I also deprecated the constructor that takes anint
and deleted the corresponding test.Improvements
I added a simple test that fails for the current implementation, but passes for the proposed implementation using the improved Kahan algorithm. Furthermore, the new implementation is substantially faster than the old one. These are the results from a benchmark adding 10M random numbers (lower is better):
Further suggestions
Looking at
RealSumTest
, I'm not entirely sure what the point of thetestDoubleSum()
andtestDoubleAdd()
methods is. It looks to me as if they are only trying to assert that the reference result obtained from summingBigDecimal
s "does the correct thing". Am I missing something here? If my interpretation is correct, I suggest to delete those tests since they are not related toRealSum
at all.