-1

I am working on C code optimization and there are many calculations which adds 1.0 to the expression which returns double value like

val =  1.0 + u[8] / c_sq + (u[8] * u[8]) / (2.0 * c_sq * c_sq) - u_sq / (2.0 * c_sq)

so I was just curious to know is there any optimization technique to improve this piece of code.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
GuruKulki
  • 25,776
  • 50
  • 140
  • 201
  • 4
    Did you measure your code's performance and determine that the adding of `1.0` was causing the predominant bottleneck? – Kerrek SB Oct 24 '13 at 00:45
  • What exactly does the line of code in your question do? What are the types of each variable? Can you reformat it to make the order-of-operation more apparent? – Dai Oct 24 '13 at 00:46
  • @Kerrek, I didn't perform this activity as the method i am working on is huge and involves lot of computation, so I was suspecting about this piece as arithmetic operations on double takes time. – GuruKulki Oct 24 '13 at 00:48
  • @Dai, all are of type double. – GuruKulki Oct 24 '13 at 00:49
  • Aren't you more worried that you make the same multiplication twice? – this Oct 24 '13 at 00:51
  • @self, please let me know any other optimization possible in this as well. – GuruKulki Oct 24 '13 at 00:52
  • 4
    Why not let the compiler optimize this one, and you go figure out where the real bottleneck is? – AShelly Oct 24 '13 at 00:53
  • (Perhaps not completely a duplicate, but eliminating the unnecessary divisions is the bulk of the work.) – Eric Postpischil Oct 24 '13 at 01:30

2 Answers2

4

That single line of code, taken on its own without any context whatsoever, is what it is. It cannot be optimized any further as long as the compiler you are using is at least doing CSE on the 2.0 * c_sq. Otherwise, that's pretty much all you can do outside of domain-specific optimizations that aren't apparent by just that code.

Jim Buck
  • 20,482
  • 11
  • 57
  • 74
  • http://en.wikipedia.org/wiki/Common_subexpression_elimination – this Oct 24 '13 at 00:55
  • Good point, added the abbreviation as a link to that page. I guess someone asking this question likely doesn't know 'CSE'. :) – Jim Buck Oct 24 '13 at 01:02
  • Division is very expensive on typical current processors, and [two divisions can eliminated from the expression](http://stackoverflow.com/a/19350996/298225). – Eric Postpischil Oct 24 '13 at 01:31
2

On typical current processors, division is time consuming; it can take dozens of CPU cycles. You can rearrange the expression to eliminate two divisions, per this answer. You can make some minor other improvements as well (which the compiler might have caught):

double t = u[8];
double v = 2*c_sq;

val = 1 + (t*(v+t) - u_sq*c_sq) / (v*c_sq);
Community
  • 1
  • 1
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • That's not a bad equation-equivalent. +1, assuming the compiler didn't already re-arrange to the equivalent. (Of course, it depends on which compiler and which settings were used for the compiler [i.e. allowing the compiler to re-arrange the calculation, since in some cases, the re-arrangement might result in slightly different results due to precison].) – Jim Buck Oct 24 '13 at 03:03