You are here:

C++/better code?

Advertisement


Question
Hi,
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;

Answer
loop 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

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.