It is well known that integer division is slow operation (typically several times slower than integer multiplication). But, if one need to perform many divide operations with a fixed divisor, it is possible to do some preconditioning on the divisor and replace "/" with multiplication and bit operations (Chapter 10 in Hacker's Delight).
As I've tested, if the divisor is a compilation-time constant, (e.g. static final long DIVISOR = 12345L;) JVM will do the trick and replace all divisions by DIVISOR with multiplication and bit operations. I'm interesting in the same kind of trick but when the divisor is known only at runtime.
For example, the following (slow) method:
void reduceArraySlow(long[] data, long denominator){ for(int i = 0; i < data.length; ++i) data[i] = data[i] / denominator; } can be replaced with something:
void reduceArrayFast(long[] data, long denominator){ SomeMagicStructure magic = computeMagic(denominator); for(int i = 0; i < data.length; ++i) // computes data[i] / denominator data[i] = doFastDivision(data[i], magic); } which must do the job much faster, since all / operations are replaced with faster operations (and also because division is not pipelined in CPUs).