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: Anonymous 2010-09-12 3:59

It's not nearly as costly as you think it is. Stop trying to microoptimize useless stuff like this at the cost of clarity. A good enough C compiler will already handle this nicely, for example, multiplication by constants is optimized by some compilers, here's one possible optimization for reg*3 on x86:

8D0440          LEA EAX,DWORD PTR DS:[EAX+EAX*2]

Which is faster than your add by 3, especially if the variable is not in a separate register.

tl;dr: Stop microoptimizing, a good compiler will take care of it.

Name: Dinga 2010-09-12 4:37

>>2
I was describing an optimizing process used by good enough compilers, not programmers.

Name: Anonymous 2010-09-12 5:49

The latter produces a different result to the former, moron.

Name: Anonymous 2010-09-12 5:51

>>4
Can't read for shit

Name: Dinga 2010-09-12 5:51

I am a professor in Angola. I know what I am doing.

Name: Anonymous 2010-09-12 6:16

(SAGE)

Name: Anonymous 2010-09-12 8:06

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

Name: Anonymous 2010-09-12 9:08

>>2
When we're talking about clarity, why would the latter be less readable than the former (ignoring the missing semicolon)?

Name: Anonymous 2010-09-12 9:08

fprintf(stdout
IHBT

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.

Name: Anonymous 2010-09-13 1:19

-funroll-loops
*VROOM VROOM*

Name: Anonymous 2010-09-13 6:29

>>11
No-one ever proposed that anyone should write the second form, only that it is a transformation a compiler can perform.

Name: Anonymous 2010-12-06 9:15

Back to /b/, ``GNAA Faggot''

Name: Anonymous 2010-12-09 21:34


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