Title: Java 18 to 30 times faster than C# when catching exceptions?
Tags: java;c#;exception
Question: I was just playing around with exceptions and ran two simple programs in C# and Java that randomly throw exception and count how many were caught in catch block. Although number of exceptions thrown is aproximitly the same, and the code is identical (at least to my limited knowledge) performance difference is huge?
What takes so long in C# to catch an exception that Java doesn't do?
Or maybe I am missing something that doesn't have anything to do with exceptions?
Here is the code:
Random random = new Random();
for (int j = 0; j < 10; j++) {
int count = 0;
long start = System.nanoTime();
for (int i = 0; i < 100000; i++) {
try {
if (random.nextInt(Integer.MAX_VALUE) % 2 == 0) {
throw new Exception();
}
}catch(Exception e) {
count++;
}
long time = System.nanoTime() - start;
System.out.printf("%d : %.1f ms%n", count ,time / 1e6);
Random random = new Random();
for (int j = 0; j < 10; j++)
int count = 0;
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 100000; i++)
try
{
if (random.Next(Int32.MaxValue) % 2 == 0)
throw new Exception();
}
catch (Exception e)
{
count++;
}
sw.Stop();
Console.WriteLine(count + ": " + sw.ElapsedMilliseconds + "ms");
First I thought that it had something to do with modulo arithmetic, so I removed all exception catching and only left:
if (random.nextInt(Integer.MAX_VALUE) % 2 == 0) {
count++;
Performance was about 3ms for each inner for loop in C#, and about 1.7ms for each inner for lopp in Java.
Now performance for both codes above:
Java                 C#
50046 : 110.5 ms     49562: 1880ms
49927 : 81.7 ms      49579: 1828ms
50053 : 90.2 ms      49944: 1857ms
49969 : 93.7 ms      49604: 1912ms
50090 : 63.9 ms      49900: 1882ms
50060 : 83.8 ms      49583: 1885ms
49904 : 71.7 ms      49698: 1899ms
50045 : 61.5 ms      49335: 1836ms
49895 : 72.1 ms      49794: 1871ms
49976 : 62.9 ms      49550: 1880ms
Specs if it matters:
Intel i3-2100 @ 3.10GHz
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
C# version 6.0
Visual Studio 2015 default compiler
Comment: Agree with Gusman, throwing exceptions for flow-control is extremely inefficient and should be avoided. Running without Exceptions, each iteration finishes within 1ms.

Comment: @stigma6 Eric Lippert's article about exceptions is one of these articles I link to people quite frequently. You should check it out: https://ericlippert.com/2008/09/10/vexing-exceptions/

Comment: I get the same results as Gusman on my PC: Takes about 500ms with the C# version.

Comment: Compile and test in release mode

Comment: On my machine it only takes an average of 500msecs using release and executiong it outside VS and 1800msecs run under VS, are you sure you're executing it in release without VS? (vs will hook to the process even if it's compiled in release)

Comment: I only can suppose it, but it must be related to the info available on the exceptions, .net should be recopilating more info than Java. Anyway exception-driven programming is really a bad way of programming, avoid it as much as you can.

Comment: @Gusman same results.

Comment: @Gusman Tried Realse, Active Realse, building for both x86 and x64 and running exe file directly from bin\release\ folder. Still the same.

Comment: @Gusman But let's say it takes 500. Even that is huge difference compared to what JVM does? Why so much? It's almost 10 times faster...

Comment: @Gusman clearly :)

Comment: @AndreAndersen I am interested to know what makes C# code so much slower than Java in this, although not real world example. I suppose that in some real world app, taking into account the article, performance with exception handling would even out?

Comment: @Kayaman Never said I was. Simple curious about drastic time difference for simingly identical code.

Comment: Clearly if you want to do Exception Driven Programming, Java is your language of choice.

Comment: Did you think you can compare the speed of the languages by taking a single minute portion like exception handling, compare that and then extrapolate the results to the whole language?



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 14, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:02/17/17', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<java><c#><exception>', 'Title': 'Java 18 to 30 times faster than C# when catching exceptions?', 'ViewCount': 111, 'id': 42300544, 'user_AboutMe': None, 'user_AccountId': 9424259.0, 'user_DisplayName': 'stigma6', 'user_Reputation': 119.0, 'user_Views': 0.0, 'user_id': 7232835.0}}