I'm not sure what kind of code has been used to benchmark that, but assuming the C++ compiler has to insert some low-level call such malloc() into the generated code to handle the new operator (or whatever), it will have to detect the return code from malloc just the same as the programmer using the C compiler. In general, I suspect C code doesn't execute error paths a lot. In a malloc example there is practically nothing to do but die if it fails. So you'd expect the C++ and C code to actually perform pretty much the same instructions -- both would do the call, and both would test for error, and in case of no error they move forward to the next user construct. In case of error, the C program would do something the programmer wrote, the C++ would do whatever magic is required to raise exception (hopefully without further memory allocations, of course). After this point, things do diverge a lot, but I think in most cases there are no errors to handle. Therefore, it would seem to me that both should perform identically, unless error returns are a common, expected result, in which case you'd have to write dispatch logic in C to deal with each error type (normally a O(log N) switch-case statement I'd guess) while the C++ compiler would probably generate code to figure out which exception handler should receive the exception. Somehow I do get the feeling that C should win in this comparison. After all, it's testing the bits of one integer, while C++ has to test exception class hierarchives. In light of this, it seems ludicruous to claim that C error handlers cost a lot of code that need to be run all the time, but somehow C++ exceptions are "free".
Copyright © 2017, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds