comma operator: keep away?

T. William Wells bill at twwells.uucp
Sat Apr 22 00:23:01 AEST 1989


In article <5053 at ingr.com> crossgl at ingr.UUCP (Gordon Cross) writes:
: I'd like to make one small observation about Mr. Wells' above comment.  In
: certain cases where execution speed is of primary importance and you don't
: have the time or energy to code in assembler (God forbid!) the vast majority
: of compilers (that I've seen anyway) tend to generate better code for a single
: complex expression that accomplishes multiple (related) tasks than for the
: equivalent multiple simple expressions.  For example an "insert into doubly
: linked list" operation on my compiler:
:
: This one used 4 instructions -
:
:   (new->next = next)->prev = (new->prev = prev)->next = new;
:
: This one used 7 instructions -
:
:   new->next  = next;
:   new->prev  = prev;
:   next->prev = new;
:   prev->next = new;

But, counterintuitively, one VAX compiler given the following:

	x = y = z = 0;

generated worse code than

	x = 0;
	y = 0;
	z = 0;

Why? Because the compiler chose to interpret the first one according
to its letter rather than its spirit, generating:

	move    z,0
	move    y,z
	move    x,y

instead of the better:

	move    z,0
	move    y,0
	move    x,0

or even:

	move    register,0
	move    z,register
	move    y,register
	move    x,register

And the compiler that did this would have compiled both of the
examples with only four instructions.

The moral? Unless you a) have reason to believe that the speed is
going to make a difference, and b) are willing to read assembly code
to verify that your coding hack really makes a difference, follow
your style rules rather than doing speed hacks.

---
Bill                            { uunet | novavax } !twwells!bill
(BTW, I'm may be looking for a new job sometime in the next few
months.  If you know of a good one where I can be based in South
Florida do send me e-mail.)



More information about the Comp.lang.c mailing list