Archived

This topic is now archived and is closed to further replies.

Question about exceptions and performance

This topic is 5669 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m using exceptions in a small project I''m currently working on at home, and I remember reading somewhere that exceptions can affect the performance of a program... I was wondering how, why and where? For example, if I have this code...
  try
  {
      CPU_intensive_function_that_throws( );
  }
  catch( ... )
  {
      // do whatever
  }
 
... would the "CPU_intensive_function_that_throws()" function take a performance hit because it is running in a try/catch block? Or is there a performance hit associated with "throw" itself? Or somewhere else? Any comment would be appreciated.

Share this post


Link to post
Share on other sites
A quote from Thinking in C++, 2nd edition:

quote:

Of course it costs something for this new feature; when an exception is thrown there’s considerable runtime overhead. This is the reason you never want to use exceptions as part of your normal flow-of-control, no matter how tempting and clever it may seem. Exceptions should occur only rarely, so the overhead is piled on the exception and not on the normally executing code. One of the important design goals for exception handling was that it could be implemented with no impact on execution speed when it wasn’t used; that is, as long as you don’t throw an exception, your code runs as fast as it would without exception handling. Whether or not this is actually true depends on the particular compiler implementation you’re using.

Share this post


Link to post
Share on other sites
quote:
Original post by i1977
So only when an exception is actually thrown does a performance hit occur. Good, that''s what I wanted to know. Thanks!

Fred



Read the last sentence again.

Share this post


Link to post
Share on other sites
I haven''t timed it specifically, but, for example, allowing exceptions keeps my functions from having to return an extra value, I would think the savings from this would outweigh the performance loss.

I guess just try timing it yourself.


void CPUIntensiveFunc1()
{
if(blah)
throw BlahExc();
//...
}

ErrorCode CPUIntensiveFunc2()
{
if(blah)
return BlahErrorCode;
//...
return OkErrorCode;
}


If you nest a lot of these functions (say CPUIntensiveFunc calls OtherCPUIntensiveFunc, which calls YetAnotherCPUIntensiveFunc, etc. for several stages), you get a lot of overhead when returning all those error codes, while with exception handling, nothing is returned unless an error actually occurred (a rare event). Theoretically. To be sure, just try timing two functions like the ones above--or even better, two deep nests of functions like the ones above. Or, even better than that, write a small application twice, once using exceptions and once without (otherwise identical), and time them.

The benefits of exception handling to code design, to me, would be worth a tiny performance loss.

---
blahpers

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
(This post is refering to VC++ 6 only, but I suspect other compilers are similer)

Blaphers: You actualy end up with more of an overhead by using exceptions then not. Theres essentialy an exception frame that gets pushed on the stack for every function call, and every try block, so that it constnatly knows where to return to if an exception happens. EVERY function, not just ones that use exceptions. This results in something like 12 more asm instructions per function call..I dont remember the exact number sorry.

If you want to see details...have it dump out the asm with source code listings. Make a simple program, with a simple function (ie, int f(){return 3;}) Compile, save asm somewhere else. Turn off exceptions, recompile, compare outputs. Debug mode I believes includes a check for the exception stack to include its intact, so its overhead is considerably worse.

Either way, I didn''t feel the overhead was worth it. Also ended up cutting off roughly 10% of my exe size at the time (granted, it was very early in that program, so that much size reduction may be exagurated, but..)

Results may vary on other compilers, but since an exception stack is needed, I dont think it''ll be too much different.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Results may vary on other compilers, but since an exception stack is needed, I dont think it''ll be too much different.

From all the tests I''ve done (mostly looking over assembly output), GCC doesn''t seem to add any overhead except when you actually throw an exception. The executable size does grow some, but that''s to be expected. The information I''ve found online supports my conclusions about GCC. I haven''t bothered to look for information on other compilers though.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Null and Void: Hrm...there has to be some overhead for keeping track of the stack unwind though. I''m sure VC++6 isn''t the best way of doing it (nor nessarily even close...and that''s not meant as a MS flame, just that nothings perfect), but I''m sure there is SOME overhead, even when not calling exceptions, for other compilers as well. This is because it has to keep track of the exception stack somehow, but I dont think VC is doing it the best way possible.

Anyway, my point is exceptions aren''t 100% free even not throwing anything, but possibly well worth it.

On a related note, if GCC does manage to do it completely overhead free, I''d be curious how its done.

Share this post


Link to post
Share on other sites
I've been messing around more just now, and it seems that GCC does push something in functions that can throw an exception. I'm not sure exactly which functions that does apply to though. It looks as if it does computer quite a lot at compile time though (it adds a couple data sections to 'precompute' exception handling from what I've read, and I do notice some tables in the assembly output, but I'm not completely sure how this works). I'm sure this changes depending on what platform GCC is compiling for.
quote:
Original post by Anonymous Poster
Anyway, my point is exceptions aren't 100% free even not throwing anything, but possibly well worth it.

Yes, I agree. I decided that they were worth it for one of my larger OO projects. There's no reason that an exception should be thrown during that projects runtime unless something has gone horribly wrong, so I decided they'd make a good complement to my debugging system (allowing me to do stack tracing and all that ).



[edited by - Null and Void on June 5, 2002 2:57:51 AM]

Share this post


Link to post
Share on other sites