2 ms·
Your benchmark supports the position that exceptions should be used instead of return codes for better average case performance, at least for GCC. If your func
by Maxatar 2y ago
Your benchmark supports the position that exceptions should be used instead of return codes for better average case performance, at least for GCC.
If your function throws an exception 9% of the time, then it outperforms error codes. That's because you always pay for the cost of an error code, regardless of success, but you only pay for the cost of an exception on failure.
I suspect most functions don't throw an exception 9% of the time that they are called.
For clang, yes the situation is worse, indicating that if your function throws an exception ~3% of the time it's better to use error codes, but once again I don't think most functions throw an exception 3% of the time.
If you need to optimize for latency, or real time performance, then exceptions are not at all suitable and I think most performance sensitive developers who have those requirements already know that.
- SassyBird 2y agoIn said benchmark the function threw an exception 3906 / 1000000 = 0.4% of the time and it was from 10 to 32 times (edit: 73.2 times on my Mac Mini M1) slower on average. 9% would be much worse. EDIT: updated the ratio to reflect the GP comment instead of the SO post.