Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Induction Variable Optimizations

Name: Dinga 2010-09-12 3:50


for(j=1;j<5;++j)
 fprintf(stdout,"%d\n",j*3);


Addition is cheaper than multiplication, so the compiler can convert this fragment to the one bellow.


int s = 0;
for(j=1;j<5;++j)
{
 s += 3
 fprintf(stdout,"%d\n",s);
}


A temporary variable has been introduced in order to eliminate the multiplications.

Name: >>2 2010-09-12 9:21

>>9
The first is clear in that it will print the multiples of 3 from 1 to 5 from the first look at the code, while the second requires you to infer that it's generating then printing each multiple of 3 from 1 to 5. The difference is small, but the first is slightly more clear, if not by much.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List