Intel® C++ Compiler
Support and discussions for creating C++ code that runs on platforms based on Intel® processors.
Announcements
The Intel sign-in experience is changing in February to support enhanced security controls. If you sign in, click here for more information.
7749 Discussions

long expression or short expression?

benjamin_gu
Beginner
221 Views
Hi,

i have a question on how to optimize some code i am working on. The biggest part of the code is a function to compute some quantities, no loops, just lots of float point computations (over 2000 lines). Most of these expression are very long and complicated, such as,

x=a * (0.5 * b * c * d + e * f);

Now i am wondering if i should break this long expression to a bunch of short expressions, like

x1=b*c;
x2=0.5*d;
x3=e*f;
x4=x1*x2;
x5x3+x4;
x=a*x5;

I hope by doing so, compiler/cpu is more likely to execute more instructions in one clock cycle? But is this true or the intel compiler does this for me already? I will really appreciate any advice?

Best,
Ben
0 Kudos
1 Reply
Dale_S_Intel
Employee
221 Views
The short answer is no, that particular change in your code should not affect the performance. As far as that kind of change, just go with what is cleanest and easiest to read. That kind of change would fall under the category of "local optimizations" and they're quite well understood by the compiler.
It is hard to say what would help your code without seeing it. You mention that there are no loops, how about if's? Is your large block of code part of a loop itself? Or is it called from a loop? Is your total application large or small? What kind of processor are you running on?
If you want to optimize this code, you should probably start with a performance tool such as VTune to get an idea where the specific hotspots are in the code and go from there.
Dale
Reply