944,196 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1576
  • C RSS
Apr 26th, 2007
0

Performance Issue

Expand Post »
Hi Friends,
Can any of you pls tell me (in detail) which of the following options will be faster?

Option 1:
  1. int fun()
  2. {
  3. if( !cm_aObject )
  4. return -1;
  5. return cm_aObject->getNum();
  6. }

Option 2:
  1. int fun()
  2. {
  3. try{
  4. return cm_aObject->getNum();
  5. }catch(...){
  6. return -1;
  7. }
  8. }

Thanx,
Amit
Similar Threads
Reputation Points: 14
Solved Threads: 3
Light Poster
amt_muk is offline Offline
48 posts
since May 2005
Apr 26th, 2007
0

Re: Performance Issue

Option 2 is slower because there is a lot more code to be executed.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Apr 26th, 2007
0

Re: Performance Issue

That doesn't mean you should avoid exception handling; in fact the performance loss is so minimal, it's hardly worth taking that into account when trying to decide when and where to use exception handling. Option 2 is by far the most robust.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Apr 27th, 2007
0

Re: Performance Issue

Thanx to both of you.

Amit
Reputation Points: 14
Solved Threads: 3
Light Poster
amt_muk is offline Offline
48 posts
since May 2005
Apr 27th, 2007
0

Re: Performance Issue

in fact the performance loss is so minimal, it's hardly worth taking that into account when trying to decide when and where to use exception handling.
not true. exceptions are designed with the idea that
a. you do not have to pay a high performance penalty in the event of there being no error
b. if there is an error, you are willing to spend time recovering from it.
c. exceptions, as the name suggests, are used to indicate exceptional conditions that do not occur in the normal course of events.

Ancient Dragon's statement is accurate; the easiest way to check it out is
a. generate the assembly code for both versions and have a look at it.
b. write a test driver and time the performance on your implementation.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: C array I/O
Next Thread in C Forum Timeline: Np-complete array generator





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC