简体   繁体   中英

Strange compiler speed optimization results - IAR compiler

I'm experiencing a strange issue when I try to compile two source files that contain some important computing algorithms that need to be highly optimized for speed.

Initially , I have two source files, let's call them Ac and Bc , each containing multiple functions that call each other (functions from a file may call functions from the other file). I compile both files with full speed optimizations and then when I run the main algorithm in an application, it takes 900 ms to run.

Then I notice the functions from the two files are mixed up from a logical point of view, so I move some functions from Ac to Bc ; let's call the new files A2.c and B2.c . I also update the two headers Ah and Bh by moving the corresponding declarations.

Moving function definitions from one file to the other is the only modification I make!

The strange result is that after I compile the two files again with the same optimizations, the algorithm now takes 1000 ms to run.

What is going on here?

What I suspect happens: when functions f calls function g , being in the same file allows the compiler to replace actual function calls with inline code as an optimization. This is no longer possible when definitions are not compiled at the same time.

  1. Am I correct in my assumption?
  2. Aside from regrouping the function definitions as it was before, is there anything I can do to obtain the same optimization as before? I researched and it seems it's not possible to compile two source files simultaneously into a single object file. Could the order of compilation matter?

As to whether your assumption is correct, the best way to tell is to examine the assembler output, such as by using gcc -S or gcc -save-temps . That will be the definitive way to see what your compiler has done.

As to compiling two C source files into a single object file, that's certainly doable. Just create a AB.c as follows:

#include "A.c"
#include "B.c"

and compile that.

Barring things that should be kept separate (such as static items which may exist in both C files), that should work (or at least work with a little modification).

However, remember the optimisation mantra: Measure, don't guess! You're giving up a fair bit of encapsulation by combining them so make sure the benefits well outweigh the costs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM