On Thursday 28 July 2011 06:22:10 Gene Buckle wrote: > On Thu, 28 Jul 2011, Jari Häkkinen wrote: > >>> Are you sure about that? I just tried it with a little example and > >>> at least gcc compiles both variants to the exact same assembly > >>> code. Tried it with and without -O2. > >> > >> That would freak me out. Doesn't "++j" mean "increment j, then test" > >> whereas "j++" means "test j, then increment"? > > > > No, for a for loop > > > > for ( [1]; [2]; [3] ) > > > > where [3] is ++j will increment j before use. However, in an > > if-statement the complete statement [3] is evaluated before the test [2] > > is done. If the compiler is smart it will produce the fastest binary > > code regardless ++j or j++. However, if the [3] is more complicated like > > a hypothetical i = ++j + k the compiler will most probably generate > > different binary code (compared to i = ++j + k). > > Right, but j++ will increment _after_ it's used, correct? So how could > ++j vs j++ generate the same assembly code and be correct?
Because the for loop is a case where it simply doesn't matter. Translate the for to a while: int i = 0; while (i < 10) i++; is just the same as: int i = 0; while (i < 10) ++i; because the value of the incrementing expression is not used anywhere. The compiler recognizes this as one of the simplest cases of optimization. Stefan ------------------------------------------------------------------------------ Got Input? Slashdot Needs You. Take our quick survey online. Come on, we don't ask for help often. Plus, you'll get a chance to win $100 to spend on ThinkGeek. http://p.sf.net/sfu/slashdot-survey _______________________________________________ Flightgear-devel mailing list Flightgear-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/flightgear-devel