|
|
|
Back to newsletter 028 contents
Extraordinarily, the Java JIT Compiler optimization which enabled this benchmark to work as listed here has been backed out in version 1.4.2 of the JDK. So sadly, this benchmark only works with 1.4.1 JDK, which unfortunately makes this already controversial page somewhat useless.
If you search the internet for comparisons of Java against other languages, you will see many advantages Java has. Cross platform portability, ease of development, garbage collection, a strong security model, and much more. But the most important feature is almost never mentioned. Java is by far the fastest computer language ever invented. I'm not talking 10% faster. I'm not talking 50% faster. I'm talking hundreds of times faster than any other language. This is hugely simple to demonstrate, with a test anyone can perform on any machine. In any language, simply time how long it takes to run a simple empty loop for a very large number of iterations. You can't get a simpler test than that! Here are the results for a few languages, using a loop iteration count of 10 000 billion iterations:
Language | Time taken to run a simple empty loop test for 10 000 billion iterations |
Java | Under one second |
Perl | approx. 1 month |
C/C++ | approx. 1 month |
C# | approx. 1 month |
Assembler | approx. 1 month |
Fortran | approx. 1 month |
Ada | approx. 1 month |
Basic (e.g. Visual Basic) | approx. 1 month |
You name it | approx. 1 month or greater |
Other languages | approx. 1 month or greater |
Naturally, these results are astonishing. But don't take my word for it. Run the test yourself. In the next few sections I provide source code and methodology for the test in several languages. You can see by reading through these sections that there is no complexity involved in running the test in any language.
1. Create a simple test class which times a loop. The Java definition is
public class Loop { public static void main(String[] args) { //10 000 billion iterations long time = System.currentTimeMillis(); int REPEAT1 = 1000 * 1000; int REPEAT2 = 1000 * 1000 * 10; for (int i = 0; i < REPEAT1; i++) { for (int j = 0; j < REPEAT2; j++) { //do nothing } } time = (System.currentTimeMillis() - time)/1000; System.out.println("Time taken: (in seconds) " + time); } }
2. Compile this class. In Java this is done with the command
javac Loop.java
assuming the previous Java class definition is saved in a file called Loop.java
3. Run the test. I use java version 1.4 running in server mode as follows:
java -server Loop
1. Create a simple test which times a loop. The Perl definition is
#10 000 billion iterations $REPEAT1 = 1000 * 1000; $REPEAT2 = 1000 * 1000 * 10; $time=time; for($i=0; $i<$REPEAT1 ;$i++) { for($j=0; $j<$REPEAT2 ;$j++) { #Do nothing } } $time=time-$time; print "Time taken: (in seconds) ",$time, "\n";"
2. Run the test. Assuming the previous Perl code definition is saved in a file called loop.perl, the test is
perl loop.perl
1. Create a simple test which times a loop. The C# definition is
using System; class Loop { static void Main() { DateTime start = DateTime.Now; int REPEAT1 = 1000 * 1000; int REPEAT2 = 1000 * 1000 * 10; for (int i = 0; i < REPEAT1; i++) { for (int j = 0; j < REPEAT2; j++) { //do nothing } } TimeSpan time = DateTime.Now - start; Console.WriteLine("Time taken: (in seconds) {0}", time.TotalMilliseconds/1000); } }
2. Run the test.
csc loop.cs
The following test can be used to test both C and C++.
1. Create a simple test which times a loop. The C/C++ definition is
#include <sys/time.h> main(int argc, char *argv[]) { int i, j, REPEAT1, REPEAT2; struct timeval before, after; void *tzp; /*10 000 billion iterations*/ tzp = 0; before = (struct timeval*) malloc(sizeof(struct timeval)); after = (struct timeval*) malloc(sizeof(struct timeval)); gettimeofday(&before, &tzp); REPEAT1 = 1000 * 1000; REPEAT2 = 1000 * 1000 * 10; for (i = 0; i < REPEAT1; i++) { for (j = 0; j < REPEAT2; j++) { //do nothing } } gettimeofday(&after, &tzp); printf("Time taken (in seconds): %ld\n", (after->tv_sec-before->tv_sec)); }
2. Compile this class. In C/C++ this is done with the command
<compile> loop.c
assuming the previous C/C++ code definition is saved in a file called loop.c, and replacing <compile> with the name of your compiler, e.g, gcc, cc, etc
3. Run the test.
a.out
The JavaPerformanceTuning.com team
Back to newsletter 028 contents