Besides divmod
being a native instruction on many chipsets, it also is a lot easier on the eyes when subdividing a number in multiple different denominations
(which happens with e.g. milliseconds -> datetime conversion, or cents -> coin denomination conversion).
So is there a divmod
that returns both the result of the division and of the remainder at the same time?
The HotSpot JIT compiler will replace division and modulo operations against the same arguments with a single divmod operation, if supported. So while this may not address the readability concerns, you don't need to worry about performance.
From the OpenJDK 9 source code:
By using diagnostic options to print the generated JIT instructions, I was able to see that a method that used both
idiv
andirem
instructions at the C1 optimization level used only a singleidiv
instruction at the C2 level.