Cryptic code == Optimized code ? YES : NO ;
Charudutta S. Palkar
csp at gtenmc.UUCP
Tue Sep 11 07:36:32 AEST 1990
C is a very good language ie to say it good to good programmers and
bad to bad programmers.
I would like to discuss a few issues that I have noticed during programming
in C.
Note : sections of examples are logically equivalent
Example 1: Variable k is of type int.
a ) if ( a > b )
k = 7;
else
k = 5;
b ) k = a > b ? 7 : 5;
c ) k = ( a > b ) * 2 + 5;
Example 2: Variables a and b are of the same type either int , char , float.
a ) a = a + b;
b = a - b;
a = a - b;
b ) a = a - ( b = ( a = a + b ) - b );
c ) a -= b = ( a += b ) - b;
Example 3: Variables a and b are of the same type either int , char
a ) a = a ^ b;
b = a ^ b;
a = a ^ b;
b ) a = a ^ ( b = b ^ ( a = a ^ b ));
c ) a ^= b ^= a ^= b;
Example 4: Variables a and b are pointers to structures with
self referential pointers as fields.
typedef struct fool
{
struct fool *nxt , *prv;
char data;
} node , *ptr;
ptr a, b;
a ) b->prv = a;
b->nxt = a->nxt;
b->nxt->prv = b;
a->nxt = b;
b ) ( b->nxt = ( b->prv = a )->nxt )->prv = a->nxt = b;
The functions of examples 2 & 3 is to interchange values 2 variables
of numeric type. Example 4 is insertion of a node in a circularly
doublely linked list.
My questions are :
1 ) Will the code generated by a non-optimizing comiler be more
more optimised as a set of statements get combined into one
expression.
2 ) Will the same happen even with an optimizing compiler.
3 ) Should such kind of compaction be favoured for development.
Thanx In advance.
C S Palkar - csp at gtenmc.gtetele.com
csp at gtenmc.UUCP
PS : Any mistakes in the above code spotted , be pointed out and
other such examples I would like to know of.
" I only speak for myself "
K&R C > ANSI C.
More information about the Comp.lang.c
mailing list