# Wednesday, 25 June 2008
« Useful Tool | Main | Exception Performance Part 2 »
Exception Performance Part 1

One area where IKVM performance is much worse than HotSpot is in throwing and catching exceptions. This blog is the first of three that will look into why this is the case.

We start out with two microbenchmarks to highlight the differences.

Java

public class ExceptionPerf1 {
  public static void main(String[] args) {
    long start = System.currentTimeMillis();
    for (int i = 0; i < 100000; i++) {
      try {
        Integer.parseInt(null);
      }
      catch (NumberFormatException x) {
      }
    }
    long end = System.currentTimeMillis();
    System.out.println(end - start);
  }
}

C#

using System;

class ExceptionPerf1 {
  public static void Main(string[] args) {
    int start = Environment.TickCount;
    for (int i = 0; i < 100000; i++) {
      try {
        throw new Exception();
      }
      catch (Exception) {
      }
    }
    int end = Environment.TickCount;
    Console.WriteLine(end - start);
  }
}

Results:

      HotSpot 1.6 x86     .NET 1.1 SP1     .NET 2.0 SP1 x86     Mono 1.9 x86
Java* 111 14743 3590 537
C#   11139 2605 187


*.NET/Mono results with IKVM 0.36

This shows that the situation on the CLR is pretty bad. If you care about exception throwing performance, please complain to Microsoft instead of to me. Although I expect that they'll tell you not to throw so many exceptions. ;-)

On the next episode we'll see that the above microbenchmark is actually a best case scenario for IKVM and we'll see how things can get much worse...

Wednesday, 25 June 2008 12:40:19 (W. Europe Daylight Time, UTC+02:00)  #    Comments [3]
Wednesday, 25 June 2008 16:22:52 (W. Europe Daylight Time, UTC+02:00)
Well: You should not throw so many exceptions ;)

Afaik this can be somewhat of a problem. As you said: In normal .Net you never need to raise an exception in normal code flow. However in Java this is quite common.

Interested to see the second part ;)
Foxfire
Wednesday, 25 June 2008 17:25:30 (W. Europe Daylight Time, UTC+02:00)
Perhaps the problem come from creating the Exception.

Have you play the test with an Exception created outside the for loop ?

JCS
JCS
Sunday, 20 July 2008 06:14:07 (W. Europe Daylight Time, UTC+02:00)
Wow i never thought mono would have such performance!

Btw i only did .net sofar, i had to google for the answer to your anti spam question :)
alwin
Name
E-mail
Home page

I apologize for the lameness of this, but the comment spam was driving me nuts. In order to be able to post a comment, you need to answer a simple question. Hopefully this question is easy enough not to annoy serious commenters, but hard enough to keep the spammers away.

Anti-Spam Question: What method on java.lang.System returns an object's original hashcode (i.e. the one that would be returned by java.lang.Object.hashCode() if it wasn't overridden)? (case is significant)

Answer:  
Comment (HTML not allowed)  

Live Comment Preview