-
Enhancement
-
Resolution: Not an Issue
-
P4
-
None
-
1.4.2
-
x86
-
windows_xp
Name: jl125535 Date: 04/08/2004
A DESCRIPTION OF THE REQUEST :
The modulo / remainder operator is 3x slower under Java than under C. I've run identical testcases under VC++ 7.1 (release mode, -O2) versus Java (-server) and ran into these results.
JUSTIFICATION :
Computational, scientific algorithms make heavy use of the modulo operator. The poor performance of this operator makes it extremely unlikely that Java will be used for these kinds of applications. This is exactly the case in my company; I am trying to convince them to move their flagship product from C to Java but it is computational-intensive in nature and these kinds of issues makes it impossible for them to consider Java.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
At most, a 10% performance overhead compared to C.
ACTUAL -
Java performs 3x worse than C.
---------- BEGIN SOURCE ----------
-----------Begin Java source code--------
import java.util.Random;
public class modulo
{
public static void main(String[] args)
{
int first;
int second;
int result = 0;
Random random = new Random(1024);
long beginTime, endTime;
beginTime = System.currentTimeMillis();
for (int i=0; i<100000000; ++i)
{
first = random.nextInt();
do
{
second = random.nextInt();
} while (second==0);
result += first % second;
}
endTime = System.currentTimeMillis();
System.out.println("result=" + result);
System.out.println("time=" + (endTime - beginTime) + "ms");
}
}
-----------End Java source code--------
Java output:
result=661355167
time=29656ms
-----------Begin C source code--------
#include "time.h"
#include "stdio.h"
#include "stdlib.h"
int main(int argc, char** argv)
{
int result = 0;
int first, second;
clock_t beginTime, endTime;
srand(1024);
beginTime = clock();
for (int i=0; i<100000000; ++i)
{
first = rand();
do
{
second = rand();
} while (second==0);
result += first % second;
}
endTime = clock();
printf("result=%d\n", result);
printf("time=%lu ms\n", (endTime - beginTime));
}
-----------End C source code--------
C output:
result=1265760437
time=11406 ms
NOTE: The 'result' difference between C and Java due to the fact that Java produces different random numbers than C for the same seed number. This should not affect the underlying test too much.
---------- END SOURCE ----------
(Incident Review ID: 245491)
======================================================================