Java Performance Tuning

Java(TM) - see bottom of page

|home |services |training |newsletter |tuning tips |tool reports |articles |resources |about us |site map |contact us |
Tools: | GC log analysers| Multi-tenancy tools| Books| SizeOf| Thread analysers|

Our valued sponsors who help make this site possible
JProfiler: Get rid of your performance problems and memory leaks! 

Question of the month: Java performance vs C/C#/VB/Perl/... March 31st, 2003

JProfiler
Get rid of your performance problems and memory leaks!


Java Performance Training Courses
COURSES AVAILABLE NOW. We can provide training courses to handle all your Java performance needs

Java Performance Tuning, 2nd ed
The classic and most comprehensive book on tuning Java

Java Performance Tuning Newsletter
Your source of Java performance news. Subscribe now!
Enter email:



JProfiler
Get rid of your performance problems and memory leaks!


Back to newsletter 028 contents

How does Java" performance compare to C/C++/C#/Perl/VB/FORTRAN/(any language of your choice)?

Next month's Question of the Month deals with whether this was a valid benchmark. You may wish to read it first

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:

LanguageTime taken to run a simple empty loop test for 10 000 billion iterations
JavaUnder one second
Perlapprox. 1 month
C/C++approx. 1 month
C#approx. 1 month
Assemblerapprox. 1 month
Fortranapprox. 1 month
Adaapprox. 1 month
Basic (e.g. Visual Basic)approx. 1 month
You name itapprox. 1 month or greater
Other languagesapprox. 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.

Java benchmark

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

Perl benchmark

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

C# benchmark

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

C/C++ benchmark

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


Last Updated: 2017-11-28
Copyright © 2000-2017 Fasterj.com. All Rights Reserved.
All trademarks and registered trademarks appearing on JavaPerformanceTuning.com are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. JavaPerformanceTuning.com is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
URL: http://www.JavaPerformanceTuning.com/news/qotm028.shtml
RSS Feed: http://www.JavaPerformanceTuning.com/newsletters.rss
Trouble with this page? Please contact us