Object Files Slow?

Hello,

I've run into a strange "bug" today. I'm writing a simple vector library for C. I mostly wanted to do this as a self-examination, but I'm running into a performance issue. My vector library is contained within 2 files. "vector.h" and "vector.c" essentially.

I wanted to mimic behavior I saw in another library, where you can compile the library along with the main if a macro is defined. In my library, if you define BNP_VECTOR_IMPLEMENTATION it does just that. I've been using this with no issues for a while.

So I decided, that some people wouldn't want to use that. So I tried compiling the two files separately. Oddly, I am getting terrible performance. When I revert back, the issues go away. I was wondering if anyone has any guidance?

Here's how I compile seperately:
g++ -O3 main.c vector.c
clang -O3 main.c vector.c
Runtime: ~2000-3000ms

Here's how I compile (when I have BNP_VECTOR_IMPLEMENTATION defined):
g++ -O3 main.c
clang -O3 main.c
Runtime: ~300-400ms

I have my code on GitHub if anyone has the patience to view it:
https://www.github.com/bnpfeife/bnp

Thanks!

EDIT:
Some more puzzling information. When I use "gcc" instead of "g++" it runs at 2000-3000ms. Strange.
Last edited on
The compiler is probably only able to inline the calls when everything is in the same translation unit.
> Here's how I compile seperately: g++ -O3 main.c vector.c

Try turning on link-time optimisation.
g++ -std=c++14 -Wall -Wextra -pedantic-errors -flto -O3 main.c vector.c
https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Optimize-Options
Thanks for the help! -flto was exactly what I needed.
Topic archived. No new replies allowed.