> I can't understand CW, I can't "optimize" my code because the slower one > is faster :( > > Look at that, this is some kind of F-S dithering: > > The original one, math involved: > > BUF(x+dir,0)-=(7*d)>>4; if(BUF(x+dir,0)<0)BUF(x+dir,0)=0; > > "Optimized" one, all possible values are computed and placed in const
> BUF(x+dir,0)-=fs7[d]; if(BUF(x+dir,0)<0)BUF(x+dir,0)=0; So in summary, you've changed '(7*d)>>4' to 'fs7[d]', where fs7 is a table which has pre-calculated the results of the unoptimised version. "(7*d)>>4" consists of a multiply and a shift. "fs7[d]" is converted by the compiler into "*(fs7 + (sizeof(Int8) * d))". "*(fs7 + (sizeof(Int8) * d))" consists of a multiply, an add, and then a memory lookup. That's going to be substantially slower than just doing the multiply and shift. The compiler may be able to optimise out the 'sizeof(Int8)' multiplication, since that evaluates to a multiplication by one. I haven't checked the output of CodeWarrior to see whether it actually does this, but with optimisations turned off I'd be quite surprised if it actually did. Regardless, even with the 'sizeof(Int8)' multiplication optimised out, you'd still have the memory lookup and the add. Now, I don't know much about Palm archetecture so I may be entirely wrong here, but I'd expect a multiply and shift to still be less expensive than an add and a memory lookup. -- For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/
