Performance Issue

Reply

Join Date: May 2005
Posts: 48
Reputation: amt_muk is an unknown quantity at this point 
Solved Threads: 3
amt_muk amt_muk is offline Offline
Light Poster

Performance Issue

 
0
  #1
Apr 26th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Performance Issue

 
0
  #2
Apr 26th, 2007
Option 2 is slower because there is a lot more code to be executed.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Performance Issue

 
0
  #3
Apr 26th, 2007
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.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 48
Reputation: amt_muk is an unknown quantity at this point 
Solved Threads: 3
amt_muk amt_muk is offline Offline
Light Poster

Re: Performance Issue

 
0
  #4
Apr 27th, 2007
Thanx to both of you.

Amit
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Performance Issue

 
0
  #5
Apr 27th, 2007
Originally Posted by joeprogrammer View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC