Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Speeding code

Author: Dieter Buerssner

Date: 12:05:49 05/12/03

Go up one level in this thread


Typically, you need to define the functions first before you can inline them. So
(totally untested code)

#include <stdio.h>
int square(int x)
{
  return x*x;
}

int main(void)
{
  int i, sum = 0;
  for (i=1; i < 10; i++)
   sum += square(i);
  printf("sum = %d\n", sum);
  return 0;
}

Here most proabably, square will get inlined (when you use the right ompiler
options).

But in

#include <stdio.h>

int square(int);

int main(void)
{
  int i, sum = 0;
  for (i=1; i < 10; i++)
   sum += square(i);
  printf("sum = %d\n", sum);
  return 0;
}

int square(int x)
{
  return x*x;
}

Man compilers (all that I tried) will not be able to inline it.

BTW. For my code, typically letting inline the compiler as much as he can, will
yield in slower code on x86 hardware. Typically functions are a bit more
complicated than square(). The compiler must do compromises, especially on
platforms with relatively few free registers available. When the compilere
inlines, he may make a decision to keep some vars of the caller in registers,
that the callee has not available now, and makes it slower by this.

I think, it is no coincidence, that compilers do not default typically to inline
functions. If it allways would be better, they would default for this.

Regards,
Dieter



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.