Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Multiplicat...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 8 of 9 Topic 44253 of 48417
Post > Topic >>

Re: Multiplication Optimization

by Andrey Tarasevich <andreytarasevich@[EMAIL PROTECTED] > Mar 31, 2008 at 11:30 AM

walnutmon wrote:
> That makes sense, what I was actually getting at is this though... I
> wasn't holding off, I simply didn't realize it at the time... At
> compilation time does C++ optimize such an instruction into the more
> simple version that I explicitly stated in the second example?

I what you have in your code is explicitly

   int x = a * b * c * d * e;

and there's no pre-calculated value for 'b * c * d * e' then the 
compiler cannot optimize it into what you started in your second example 
simply because it will not achieve anything. There's absolutely no 
difference in doing the above, or doing

   int f = b * c * d * e;
   int x = a * f;

Now, if somewhere earlier you calculated 'f' yourself

   int f = b * c * d * e;

and then, later, you calculate 'x' as

   int x = a * b * c * d * e;

the compiler could be smart enough to optimize the second into

   int x = a * f;

assuming that it it can establish the fact that 'b', 'c', 'd', and 'e' 
didn't change between these points.

Also if you do your

   int x = a * b * c * d * e;

repeatedly for different values of 'a', but the same values of 'b', 'c', 
'd', and 'e', the compiler could be smart enough to pre-calculate 'b * c 
* d * e' and use it to calculate 'x' instead of re-calculating the whole 
thing every time.

But all this relies on the details you haven't provided. Outside of 
those more specific contexts, once again, there's absolutely no 
difference between your first and the second variant and, therefore, it 
would be strange to expect the compiler to do this as an "optimization".

-- 
Best regards,
Andrey Tarasevich
 




 9 Posts in Topic:
Multiplication Optimization
walnutmon <justin.m.bo  2008-03-31 09:29:48 
Re: Multiplication Optimization
"Victor Bazarov"  2008-03-31 12:40:14 
Re: Multiplication Optimization
brian tyler <brian.tyl  2008-03-31 09:42:27 
Re: Multiplication Optimization
walnutmon <justin.m.bo  2008-03-31 09:46:45 
Re: Multiplication Optimization
Rui Maciel <rui.maciel  2008-03-31 18:10:11 
Re: Multiplication Optimization
Alan Johnson <awjcs@[E  2008-03-31 09:53:01 
Re: Multiplication Optimization
walnutmon <justin.m.bo  2008-03-31 11:18:12 
Re: Multiplication Optimization
Andrey Tarasevich <and  2008-03-31 11:30:55 
Re: Multiplication Optimization
brian tyler <brian.tyl  2008-03-31 11:44:07 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Nov 21 9:23:56 CST 2008.