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.
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.
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.
> 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++.