aspose file tools*
The moose likes Performance and the fly likes mod operator efficiency Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "mod operator efficiency" Watch "mod operator efficiency" New topic

mod operator efficiency

Paul Fenerty

Joined: May 06, 2005
Posts: 23

There is a technique for implementing a multiplicative linear congruential random number generator

x[n+1] = ( a * x[n] ) mod m (eq. 1)

in 32 bit integers that eliminates the potential for overflow in the intermediate product (a * x[n]), known as Schrage's method, and popularized in Numerical Recipes in C


Schrage�s algorithm is based on an approximate factorization of m,

m = aq + r, i.e., q = [m/a], r = m mod a (7.1.4)

with square brackets denoting integer part. If r is small, specifically

r < q, and 0 < z < m - 1,

it can be shown that both a(z mod q) and r[z/q]lie in the range

0,...,m - 1, and that

az mod m = a(z mod q) - r[z/q] if it is = 0, (7.1.5)
a(z mod q) - r[z/q] + m otherwise

Applying Schrage to the LCG, in C code, Numerical Recipes achieves

x = a * (x - (x / q) q) - r (x / q) (eq. 2)

by substituting the modulo arithmetic equivalence

x mod q = x - q[x/q]

into a preceding step, which is

x = a * (x % q) - r * (x / q) (eq. 3)

which you get by applying Numerical Recipe's (7.1.5) to the LCG (eq. 1)

So, why keep going past (eq. 3) ??

Doing so replaces the mod operator in (eq. 3) with a subtraction, a multiplication, and a division, in (eq. 2).

Is mod that expensive??

Which expression is faster in Java? Note that this code can be iterated up to near 10^18 repetitions, with a combined pair of certain such LCGs, so every cycle counts bigtime.

Paul Fenerty

Joined: May 06, 2005
Posts: 23
They also replace (x/q), which appears twice in (eq. 2) with another variable, and so there is a savings there, evaluating (x/q) only the once per iteration, in a separate expression.

Javaworld claims:

"divide remains the slow operation on most if not all architectures."


Anybody know the relative efficiency of * / and mod ??

Thanks again!
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12785
If that question was bothering me, as a first step I would write some test code and then use the javap tool to look at the bytecodes written by the compiler.
Paul Fenerty

Joined: May 06, 2005
Posts: 23
Thanks for that.

The new info that javap provides is the following:

* -> imul
/ -> idiv
% -> irem

Digging around, I (only) found a lisp implementation (ACL2), by J. Strother Moore:

that looks like this:

; (IDIV) Instruction

(defun execute-IDIV (inst th s)
(modify th s
c (+ (inst-length inst) (pc (top-frame th s)))
:stack (push (int-fix
(truncate (top (pop (stack (top-frame th s))))
(top (stack (top-frame th s)))))
(pop (pop (stack (top-frame th s)))))))

; -----------------------------------------------------------------------------

; (IMUL) Instruction

(defun execute-IMUL (inst th s)
(modify th s
c (+ (inst-length inst) (pc (top-frame th s)))
:stack (push (int-fix
(* (top (pop (stack (top-frame th s))))
(top (stack (top-frame th s)))))
(pop (pop (stack (top-frame th s)))))))

; -----------------------------------------------------------------------------

; (IREM) Instruction

(defun execute-IREM (inst th s)
(let* ((val1 (top (pop (stack (top-frame th s)))))
(val2 (top (stack (top-frame th s))))
(result (- val1 (* (truncate val1 val2) val2))))
(modify th s
c (+ (inst-length inst) (pc (top-frame th s)))
:stack (push result
(pop (pop (stack (top-frame th s))))))))

; -----------------------------------------------------------------------------

Don't know how typical this is among JVMs, but irem here is busier than the other two.

Taking some data on a P3, three samples of 2 x 10^10 iterations across each of the three versions of Schrage's version of Lehmer's mLCG come in as follows, and confirm that the modulo expression is the slowest approach:


x = a * (x - (x / q) * q) - r * (x / q); // (eq. 2)
8m 44s


int k = x / q;
x = a * (x - k * q) - r * k; // (eq. 2b)
8m 43s


x = a * (x % q) - r * (x / q); // (eq. 3)
9m 50s

... so looks like they got it right in Numerical Recipes.
Paul Fenerty

Joined: May 06, 2005
Posts: 23
um, make that 2 x 10^9 iterations ...
Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385
Maybe implement both and then run some profiling tests, i.e. how many calculations it can make in 10 min using each method?
I agree. Here's the link:
subject: mod operator efficiency