C++/better code?
Expert: vijayan - 1/20/2009
QuestionHi,
I'm new at programming but my teachers say that we need to practice to write better codes.
How could I know which code is more efficient?or faster?
int tab[20], i;
for (i=0;i<20;i++) tab[i]=i;
OR
int tab[20], i;
tab[0]=0;tab[1]=1;tab[2]=2;tab[3]=3;
... ; tab[19]=19;
Answerloop unrolling (as in the example above), is a technique that attempts optimize a program's execution speed at the expense of its code size.
most modern compilers will perform loop unrolling (and many other low level optimizations) automatically; write code that is transparent (easy to read, easy to understand, and easy to maintain) and leave low level optimizations to the compiler. focus your optimization efforts at choosing more efficient data structures and algorithms. see:
http://en.wikipedia.org/wiki/Loop_unwinding
http://en.wikipedia.org/wiki/Duff%27s_device