4.6 Java - Performance Oversight?



Thanks for your work on this book. I wanted to bring up something I noticed that I don’t think was intentional in the Java solution to 4.6.

When the long yPower is assigned, y, which is an int, is shifted as an int, so the result, in my testing, has been that y shifted overflows back to itself, and only afterwards is made a long. The result of this is that yPower starts at y instead of at an extreme, so the potential for grouping work is not actually used.

When I cast y into a long first before shifting, I see the performance gains you would as the average runtime in the EPI Judge tests drops from 2ms to 2us.