Java 200x faster exception handling

I've discovered that Java has a much faster exception handling system than C++, which suprised me since Java is managed(using Microsoft's term) and C++ is native.
The test-code I made showed that Java handles exceptions about 200 times faster than C++.

My question is why exception handling in C++ is so slow, and why is Java's handling so fast? Is there a way to make c++'s faster, or is there an other way to do it?

It might be that the reason for the slowness lies in my code, so tell me if you find a bug.

Here's my benchmark code:
(C++ code)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <windows.h>

class Exception {
public:
	Exception(std::string msg) : message(msg) {
	}

	std::string getMessage() {
		return this->message;
	}
private:
	std::string message;
};



double getTime() {
	FILETIME fileTime; 
	GetSystemTimeAsFileTime(&fileTime);

	ULARGE_INTEGER uli;
	uli.LowPart = fileTime.dwLowDateTime;
	uli.HighPart = fileTime.dwHighDateTime;

	return uli.QuadPart/1.0E7;
}

void lord() {
	throw Exception("WHJGYEHGERRORYHBADG!");
}

int main(int argc, char* argv[])
{
	double start, stop; 
	start = getTime();
	for (int i = 0; i < 10000; i++) {
		try {
			lord();
		}
		catch (Exception ex) {
		}
	}
	stop = getTime();
	double timeDiff = (stop - start);
	std::cout << timeDiff << std::endl;
	system("PAUSE");
	return 0;
}

(Java code)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.lang.management.*;
import java.io.*;

public class Benchmark {
	private long userStamp, cpuStamp;
	
	public long getCpuTime() {
		ThreadMXBean bean = ManagementFactory.getThreadMXBean( );
		return bean.isCurrentThreadCpuTimeSupported( ) ?
			bean.getCurrentThreadCpuTime( ) : 0L;
	}
	
	public void startUser() {
		userStamp = System.nanoTime();
	}
	
	public void stopUser() {
		userStamp = System.nanoTime() - userStamp;
	}
	
	public void startCpu() {
		cpuStamp = getCpuTime();
	}
	
	public void stopCpu() {
		cpuStamp = getCpuTime() - userStamp;
	}
	
	public double getUser() {
		return userStamp / 1.0E9;
	}
	
	public double getCpu() {
		return cpuStamp / 1.0E9;
	}
}

public class Client {
	public static void lord() throws Exception {
		throw new Exception("WHJGYEHGERRORYHBADG!");
	}

	public static void main(String[] args) {
		Benchmark bm = new Benchmark();
		bm.startUser();
		for (int i = 0; i < 10000; i++) {
			try {
				lord();
			}
			catch (Exception ex) {
			}
		}
		bm.stopUser();
		System.out.println(bm.getUser());
	}
}
Last edited on
this is very interesting.

I haven't tested results and experimented myself so would like to ask you what happens if you don't catch by value but by reference:

1
2
3
4
catch (Exception & e)
{

}


and also if one changes the string with which exception is set - ie don't have same string so avoid an optimisations java might be taking.

Are you sure the C++ timer code is working correctly. I don't really understand how it works but when I change the C++ timer code to something that I understand and works for me, I get that the C++ version is about 1.5 times slower than the Java version.

EDIT:
Increasing the number of loop iterations makes the Java program somewhat better compared to the C++ program but nowhere near 200 times better.
Last edited on
Your execption hander causes a new Exception (and string) to be created. Have you looked at std::exception? And are you aware of Object Slicing during exception handling? You really ought to be catching by const reference.

As Java variables are pointers to objects that are most likely destroyed after the test, and the C++ exception objects are destroyed as part of tyou're not comparing like with like.

Java doesn't really have destructors, but in C++ that's the whole point of exceptions; so again, they're not quite the same.

Exception handling is switched off by default on Microsoft compilers because it's known to have a performace impact.
Last edited on
> I've discovered that Java has a much faster exception handling system than C++

Yes, it does. Though it typically is not 'much faster'


> which suprised me

It is not surprising - as kbw points out, exception handling in languages that provide first-class facilities for managing resources (C++, D, Ada) would be more expensive than exception handling in languages that don't.


> Java handles exceptions about 200 times faster than C++.

I presume you don't have another account which goes by the name of rapidcoder. If that is the case, you might want to have a look at section 5.4 of TR 18015: http://www.open-std.org/jtc1/sc22/wg21/docs/TR18015.pdf
Topic archived. No new replies allowed.