<p>What is the best way to measure exception handling overhead/performance in C++?</p>

<p>Please give standalone code samples.</p>

<p>I'm targeting Microsoft Visual C++ 2008 and gcc.</p>

<p>I need to get results from the following cases:</p>

<ol>
<li>Overhead when there are no try/catch blocks</li>
<li>Overhead when there are try/catch blocks but exceptions are not thrown</li>
<li>Overhead when exceptions are thrown</li>
</ol>

[{'answer_id': 43288, 'author': 'Carl Seleborg', 'author_id': 2095, 'author_profile': 'https://Stackoverflow.com/users/2095', 'pm_score': 3, 'selected': False, 'text': "<p>As a suggestion: don't bother too much with the overhead when exceptions are thrown. Exception handling implementations usually make not throwing fast and catching slow. That's ok since those cases are, well, exceptional.</p>\n\n<p>Carl</p>\n"}, {'answer_id': 43292, 'author': 'Antti Kissaniemi', 'author_id': 2948, 'author_profile': 'https://Stackoverflow.com/users/2948', 'pm_score': 3, 'selected': False, 'text': '<p>Here\'s the measuring code I came up with. Do you see any problems with it?</p>\n\n<p>Works on Linux and Windows so far, compile with:</p>\n\n<pre><code>g++ exception_handling.cpp -o exception_handling [ -O2 ]\n</code></pre>\n\n<p>or for example <a href="http://www.microsoft.com/express/vc/" rel="noreferrer">Visual C++ Express</a>.</p>\n\n<p>To get the base case ("exception support removed from the language altogether"), use:</p>\n\n<pre><code>g++ exception_handling.cpp -o exception_handling [ -O2 ] -fno-exceptions -DNO_EXCEPTIONS\n</code></pre>\n\n<p>or similar settings in MSVC.</p>\n\n<p>Some preliminary results <a href="http://spreadsheets.google.com/ccc?key=pHRlLr2bT1jTBRqVC9E6bLw&amp;hl=en" rel="noreferrer">here</a>. They are probably all hokey because of varying machine load, but they do give some idea about the relative exception handling overhead. (Executive summary: none or little when no exceptions are thrown, huge when they actually are thrown.)</p>\n\n<pre><code>#include &lt;stdio.h&gt;\n\n// Timer code\n\n#if defined(__linux__)\n#include &lt;sys/time.h&gt;\n#include &lt;time.h&gt;\n\ndouble time()\n{\n    timeval tv;\n    gettimeofday(&amp;tv, 0);\n    return 1.0 * tv.tv_sec + 0.000001 * tv.tv_usec;\n}\n#elif defined(_WIN32)\n#include &lt;windows.h&gt;\n\ndouble get_performance_frequency()\n{\n    unsigned _int64 frequency;\n    QueryPerformanceFrequency((LARGE_INTEGER*) &amp;frequency); // just assume it works\n    return double(frequency);\n}\n\ndouble performance_frequency = get_performance_frequency();\n\ndouble time()\n{\n    unsigned _int64 counter;\n    QueryPerformanceCounter((LARGE_INTEGER*) &amp;counter);\n    return double(counter) / performance_frequency;\n}\n#else\n# error time() not implemented for your platform\n#endif\n\n// How many times to repeat the whole test\nconst int repeats = 10;\n\n// How many times to iterate one case\nconst int times = 1000000;\n\n// Trick optimizer to not remove code\nint result = 0;\n\n\n\n// Case 1. No exception thrown nor handled.\n\nvoid do_something()\n{\n    ++result;\n}\n\nvoid case1()\n{\n    do_something();\n}\n\n\n\n// Case 2. No exception thrown, but handler installed\n\n#ifndef NO_EXCEPTIONS\nvoid do_something_else()\n{\n    --result;\n}\n\nvoid case2()\n{\n    try\n    {\n        do_something();\n    }\n    catch (int exception)\n    {\n        do_something_else();\n    }\n}\n\n\n\n// Case 3. Exception thrown and caught\n\nvoid do_something_and_throw()\n{\n    throw ++result;\n}\n\nvoid case3()\n{\n    try\n    {\n        do_something_and_throw();\n    }\n    catch (int exception)\n    {\n        result = exception;\n    }\n}\n#endif // !NO_EXCEPTIONS\n\nvoid (*tests[])() =\n{\n    case1,\n#ifndef NO_EXCEPTIONS\n    case2,\n    case3\n#endif // !NO_EXCEPTIONS\n};\n\nint main()\n{\n#ifdef NO_EXCEPTIONS\n    printf("case0\\n");\n#else\n    printf("case1\\tcase2\\tcase3\\n");\n#endif\n    for (int repeat = 0; repeat &lt; repeats; ++repeat)\n    {\n        for (int test = 0; test &lt; sizeof(tests)/sizeof(tests[0]); ++test)\n        {\n            double start = time();\n\n            for (int i = 0; i &lt; times; ++i)\n                tests[test]();\n\n            double end = time();\n\n            printf("%f\\t", (end - start) * 1000000.0 / times);\n        }\n        printf("\\n");\n    }\n\n    return result; // optimizer is happy - we produce a result\n}\n</code></pre>\n'}, {'answer_id': 43297, 'author': 'Maximilian', 'author_id': 1733, 'author_profile': 'https://Stackoverflow.com/users/1733', 'pm_score': 2, 'selected': False, 'text': "<p>There's no really good way to meassure that in code. You would need to use a a profiler.</p>\n\n<p>This won't show you directly how much time is spent with exception handling but with a little bit of research you will find out which of the runtime methods deal with exceptions (for example for VC++.NET it's __cxx_exc[...]).</p>\n\n<p>Add their times up and you have the overhead. In our project we used vTunes from Intel which works with both Visual C++ and gcc.</p>\n\n<p>Edit: Well, if you just need a generic number that might work. Thought you had an actual application to profile where you can't just turn off exceptions.</p>\n"}, {'answer_id': 43406, 'author': 'Xavier Nodet', 'author_id': 4177, 'author_profile': 'https://Stackoverflow.com/users/4177', 'pm_score': 5, 'selected': False, 'text': '<p>Section 5.4 of the <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/TR18015.pdf" rel="noreferrer">draft Technical Report on C++ Performance</a> is entirely devoted to the overhead of exceptions. </p>\n'}, {'answer_id': 43928, 'author': 'Carl Seleborg', 'author_id': 2095, 'author_profile': 'https://Stackoverflow.com/users/2095', 'pm_score': 2, 'selected': False, 'text': "<p>Another note on exception handling performance: simple tests don't take caching into account. The try-code and the catch-code are both so small that everything fits in the instruction and data caches. But compilers may try to move the catch-code far away from the try-code, which reduces the amount of code to keep in cache normally, thus enhancing performance.</p>\n\n<p>If you compare exception handling to traditional C-style return-value checking, this caching effect should be taken into account as well (the question is usually ignored in discussions).</p>\n\n<p>Carl</p>\n"}, {'answer_id': 44764, 'author': 'Greg Whitfield', 'author_id': 2102, 'author_profile': 'https://Stackoverflow.com/users/2102', 'pm_score': 0, 'selected': False, 'text': "<p>Won't the answer depend on what cleanup has to happen as a result of a throw? If an excpetion is thrown that causes a whole load of objects to go out of scope up the stack, then that will add to the overhead.</p>\n\n<p>In other words, I'm not sure if there is a an answer to the 3rd question that is independent of the specifics of the code.</p>\n"}, {'answer_id': 46113, 'author': 'cic', 'author_id': 4771, 'author_profile': 'https://Stackoverflow.com/users/4771', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://blogs.msdn.com/freik/" rel="nofollow noreferrer">Kevin Frei</a> talks about exception handling performance cost in his speech "<a href="http://www.nwcpp.org/Meetings/2006/10.html" rel="nofollow noreferrer">The Cost of C++ Exception Handling on Windows</a>". (Under "Summary &amp; Conclusions" there is one list item that says "[Exception handling performance cost is] not always measurable".)</p>\n'}, {'answer_id': 557881, 'author': 'Arcane', 'author_id': 54811, 'author_profile': 'https://Stackoverflow.com/users/54811', 'pm_score': 0, 'selected': False, 'text': '<p>Full details on how g++ handles exceptions is shown <a href="http://www.codesourcery.com/public/cxx-abi/abi-eh.html" rel="nofollow noreferrer">here</a>.  It describes it as being for the Itanium architecture, however the general techniques used are the same.  It won\'t tell you exact overhead in terms of time, however you can glean what the rough code overhead will be. </p>\n'}]
['https://Stackoverflow.com/questions/43253', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2948/']