C Programming Skills-#4

[Note: This was originally written for "FYI", the internal newsletter for the worldwide software engineering departments of Dendrite International]

Quick Quiz: Each of these will perform exactly the same function. Which will generate the most efficient code when compiled?

int test1(int a, int b, int c)
{
	if ( (a == b) && (b ==c) )
		return(1);

	return(2);
}


int test2(int a, int b, int c)
{
	if ( (a == b) & (b ==c) )
		return(1);

	return(2);
}


int test3(int a, int b, int c)
{
	if (a == b)
		if (b ==c) 
			return(1);

	return(2);
}

Answer: Test1() and Test3() will produce, byte for byte, identical machine code outputs. This was tested on both Borland C v3.1 and Microsoft C v8.0, but I doubt the results would be different using a UNIX compiler. Often programmers lose sight of the fact that a high level language must reduce the program to machine code, and begin to think that the shorter the source code, the more efficient the program. In reality, the two forms are asking the compiler to perform exactly the same task.

Which bring us to test2(). How does that do, compared to the others? A distant third place. How can that be? It's only one character different then test1(). Yes, but it's an important character. The single ampersand says to treat the values on either side of it as numbers. However, they are logical expressions, so first we have to convert them into numbers: 0 if the expression is FALSE, 1 if TRUE. As above, the shorthand style masks some complexity. In fact, the compiler treats that code exactly as if it were written as:

test4(int a, int b, int c)
{
	int	r1,r2;

	if (a==b)
		r1 = 1;
	else
		r1 = 0;

	if (b==c)
		r2 = 1;
	else
		r2 = 0;

	if (r1 & r2)
		return(1);

	return(2);
}

Not very pretty, eh? In fact, this version was even slightly more efficient than test2(), probably because with everything out in the open, the optimizer could work with it better.

Copyright © 1994, James M. Curran