Exceptions and various compilation option speeds
Wednesday, January 6th, 2010So after some discussion yesterday with a few folks about Exceptions in C++, I decided to add a compilation flag to disable the use of exceptions in tinypyC++. The claim given to me was around these few items: 1. Exceptions slow your code down. 2. Exceptions aren’t cross-platform (in some cases). 3. Exceptions can easily add in memory-leaks.
I can’t argue with #2, although it’s not completely relevant for the platforms I’m targeting right now. #3, with the way tinypyC++ does reference counting, I wouldn’t be surprised if this could happen. #1, I figured might be true, but I wanted to know how real #1 actually was. So here are my results:
g++ flags | ms |
---|---|
(none) | 894 |
-g | 891 |
-O3 | 414 |
-DTP_NO_THROW -fno-exceptions | 894 |
-g -DTP_NO_THROW -fno-exceptions | 878 |
-O3 -DTP_NO_THROW -fno-exceptions | 378 |
In the first test I’m rendering a Julia fractal for 20 frames and averaging the time it takes to render. In the second test (below) I’m doing the same test, but I did one code optimization to remove a new/delete that was happening for each pixel – by reusing the coordinates object.
g++ flags | ms |
---|---|
(none) | 293 |
-g | 290 |
-O3 | 153 |
-DTP_NO_THROW -fno-exceptions | 304 |
-g -DTP_NO_THROW -fno-exceptions | 303 |
-O3 -DTP_NO_THROW -fno-exceptions | 112 |
I found it interesting that in the 2nd test case I did, removing exceptions actually increased the time it took to render the fractal! Not quite sure why .. hmmm. (I tried it a few times and kept getting similar results too.)
What is clear in the second test is that the compiler is able to optimize the code much more aggressively. I guess Exceptions choke up the optimizer. In all cases it seems that adding on the -g flag seems to make the code run slightly faster, surprisingly enough! (-g adds in debugger info.) In all cases, the optimizer was able to roughly double the speed of the program, and when I disable exceptions it is able to throw in even more speed.
-Phil