FindPrimes Benchmark

I’ve posted a short write-up with code of a FindPrimes Benchmark to compare Java, C++ and C# execution speeds on my Mac. Summary: C++ takes 1.5 – 2 times as long as Java and C# takes 3 times as long as Java. I didn’t expect much from C# (Mono) on the Mac, but who knew Mac Java was so good or that Mac C++ compilers/libraries were so bad?

8 thoughts on “FindPrimes Benchmark”

  1. Wow — you should send these to Apple and also feel good for paying for a Mac JRE 🙂

    The Java being faster at Linked List — is there any gc being done at all? Can it be that C does freeing manually for nothing whereas Java leaves the mess and not even the gc gets to clean up?

  2. I updated the times after finding a “Use 64-bit Arithmetic” option in the new Xcode 2.1 which uses GCC 4. Then the “obvious” memory allocation difference went away. Who knows how much work the gc is doing on in all of those calls to new, but just to be fair I tried the C++ version without any memory clean-up and the linked list test improved by half a second.

  3. But XCode is faster than CW only in the LinkedList case. And the difference is a factor 2! Surprised also that Java 1.5 is a little slower on all tests except for a small difference on LinkedList. Will run Java 1.3 and 1.4 on my machine…

  4. I found Java 1.5 to be slower when working on the GNU Chess port, also. At least it’s no slower on Windows than 1.4.

  5. Actually, I meant “at least Java 1.5 on Windows is no slower than Java 1.4 on Windows.” As opposed to the Mac, where Java 1.5 *is* slower than Java 1.4.

  6. Interesting to see the comparison between the languages and the MAC and PC platform.

    I ran the benchmark on an AMD athlon 2.4 GHz computer both under Linux (Suse 64 bit) and windows 2000. (Sun server jdk 1.5_06)
    results Linux:
    9.46, 0.09, 1.57, 1.41, 1.87, 2.08 and a total of 16.48 s
    results windows.
    13.27, 0.11, 1.97, 1.66, 2.17, 2.45 total 21.62 s

    The 64 bit virtual machine seems to be quite fast in comparison with the windows version.

    However, interpretation of benchmarks results is not always easy.
    This particular benchmark spends most of its time in the linked list finder.
    And within this module the time consuming tasks are not the creation of objects or traversing the linked list but the statement with the modulo (%) function!
    You can easily test this by doing this test twice in the code, which nearly doubles the run time.
    This function is apparently relatively slow for Long arguments.
    (Casting to temporary double variables first makes things significantly faster!)

    It is good to see that Java is equally fast as C++ or C#, and that Java on the mac is getting up to a decent speed.

  7. Thanks for the numbers Hans. I never would have guessed that modulo was the bottleneck for the linked list test. Makes me wonder why other languages haven’t done what Forth has done since the 70s: provide an operator for dividing a long int by a regular int without causing the latter to be converted to a long int first. That is, quot32 = dividend64 / dividend 32.

Comments are closed.