Slowdown problem if function C is placed in a separate file?

I'm having some weird slowdown problems in my program written in C.

I have the following code:

typedef struct {
   Uint8 r;
   Uint8 g;
   Uint8 b;
   Uint8 alpha;
} COLOUR;

COLOUR get_colour(int r, int g, int b, int alpha) {
   COLOUR colour;

   colour.r = r;
   colour.g = g;
   colour.b = b;
   colour.alpha = alpha;

   return colour;
}

Then I insert something like this into my main loop, just to reproduce my problem:

for (i = 0; i < 640 * 480; i++) {
   blue = get_colour(0, 0, 255, 255);
   yellow = get_colour(255, 255, 0, 255);
}

This works fine until it slows down.

BUT, if I translate the code for my get_colour () function into a separate .C file (I prefer to store such functions in a library), I start the slowdown. Just a simple for-loop above causes the frame rate to drop from 100 to fps to 70 frames per second.

Moving the code for my get_colour () function returns to the same .C file as the rest of the code returns the normal speed.

What causes this?

- GCC MinGW, - .

.

+4
1

- inlining vs not. , , inline .

, .h main(). -O , inilining.

bar.h:

typedef struct {
   Uint8 r;
   Uint8 g;
   Uint8 b;
   Uint8 alpha;
} COLOUR;

inline COLOUR get_colour(int r, int g, int b, int alpha) {
   COLOUR colour;

   colour.r = r;
   colour.g = g;
   colour.b = b;
   colour.alpha = alpha;

   return colour;
}

foo.c:

#include "bar.h"

int main() {
    int i;
    COLOUR blue, yellow;
    for (i = 0; i < 640 * 480; i++) {
       blue = get_colour(0, 0, 255, 255);
       yellow = get_colour(255, 255, 0, 255);
    }    
}
+3

Source: https://habr.com/ru/post/1621718/


All Articles