June 29, 2004
Java vs. C++ benchmarking
Recently, at a joint Intel/University of Washington meeting, we had a discussion in which the performance of Java was called into question. Discussions like this rarely have much ammunition to support either side. However, in this case, there seems to be some evidence that if you run your JVM correctly then Java is at least as good as, and sometimes better than C++. This evidence comes from CS major at Rensselaer Polytechnic Institute named Keith Lea and was recently featured on Slashdot. His original article is here. |
Update:
Henry just did a test that further supports this guy's conclusion. Here's the guts from him:
I just compared the following two little programs, in C and Java
(Sun 1.42, linux).
gcc CSpeedArray.c -o CSpeedArray
time CSpeedArray
real 0m11.837s
user 0m11.830s
sys 0m0.000s
gcc -O2 CSpeedArray.c -o CSpeedArray
time CSpeedArray
real 0m3.451s
user 0m3.420s
sys 0m0.030s
time java SpeedArray
real 0m3.725s
user 0m3.640s
sys 0m0.030s
I didn't believe Don when he told me that Java was now just as fast as
C... but seeing is believing. No more C / C++ for me!
-------------------------------------------------------
CSpeedArray.c
-------------------------------------------------------
#include <stdlib.h>
int main(int argc, char ** argv){
int * a;
int i;
int j;
a = malloc( sizeof(int) * 1000000 );
for (i = 0; i < 1000000; i++){
a[i] = 0;
}
for (j = 0; j < 1000; j++){
for (i = 0; i < 1000000; i++){
a[i] = a[i] + 1;
}
}
return 0;
}
-------------------------------------------------------
SpeedArray.java
-------------------------------------------------------
public class SpeedArray {
public static void main(String arg[]) {
int[] a = new int[1000000];
for (int i = 0; i < 1000000; i++){
a[i] = 0;
}
for (int j = 0; j < 1000; j++){
for (int i = 0; i < 1000000; i++){
a[i] = a[i] + 1;
}
}
}
}