[IPOL discuss] Fast DCT using FFTW3

Miguel Colom Miguel.Colom at cmla.ens-cachan.fr
Wed Feb 29 10:12:10 CET 2012


> Hi Miguel,
>
> Yes, your example works according to your analysis, but only because your
> function f is simple enough and the compiler can inline it instead of
> doing a
> function call. But I think if f is complex enough, even it has no side
> effect
> (no printf, independent of any global variable), the compiler would not be
> able to inline it and it would still be called 3 times, even though the
> result
> is the same at each call.

Hi,
of course, the compiler has a limit for the type of optimization it can
perform.

The point here is, in my opinion, that sometimes there's no need to
sacrifice the readability of the code in the sake of gaining some
nanoseconds, because the compiler can take care of it. For example,
there's no need to store 2*sqrt(2) as a constant if it's used many times,
because it's inlined by the compiler.

Even in a program like the following, the function g(x) is inlined in a
single instruction: "movl $253, 8(%esp)" when the compiler knows the
argument (23).

int g(int x) {
  int sum = 0;
  int i;
  for (i = 0; i < x; i++)
    sum += i;
  return sum;
}

int main(int argc, char *argv[]) {
 int result;
 int x = 23;

 result = g(x);

 printf("The result is %d\n", result);
 return 0;
}

Of course, if the expression is more complex (like computing a sum of
squares, for example), the function is called several times.

About the number of calls the compiler would invoke a function, it's not
ensured and depends on the level of optimization.

For example, let's consider a boolean function: "f(x) && g(y) && h(z)". If
f(x) is false, then g(y) and h(z) won't be called/evaluated.

My conclusion is that we should optimize the code my ourselves if these
optimizations aren't trivial. If so, the compiler can handle them and
there's no need to avoid writing very clear and clean code.

Best,
Miguel




More information about the discuss mailing list