IdeaMonk

thoughts, ideas, code and other things...

Wednesday, June 06, 2007

C takes logical shortcuts.

Look at this :
if ( a && b && c && d && e && f && g && h &&  i  ||  j ) {
.... do the job ...
}
One would think that this is a very long condition to evaluate. You would say it will take time to go through checking all of a...j variables. But, C doesn't like to toil uselessly. Just in case b is FALSE, C would immediately stop evaluating other variables d,e,f,g,h which are connected by logic AND. It would simply evaluate j in this case and apply OR.
So, in this case performance of program will definitely depend on what values have a,b,c,d,e,f,g,h have got. The earlier C finds a FALSE among them, the faster will the code run!
One can even call this short circuiting!

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home