Search code examples
c++static-linking

Is static linking slower than local code


I am writing some statistical software (C++) where speed is the top priority; second to that is maintainability.

Separately, I have written a static library which bundles together a collection of algorithms that I use routinely. Many of my programs use this library, so maintaining these algorithms independently works perfectly for me. The new statistical software uses this library as well.

While debugging the new software, I commented out a few calls to some simple (computationally-speaking) functions from the library. The speed increase which resulted from omitting the calls seemed disproportionate - raising this question:

Q. Are there execution time penalties associated with calling statically linked code, beyond those which would result from compiling the same code locally?

Note - this is tough to search for. All of my internet searches resulted in comparisons of static versus dynamic links - which doesn't pertain.

Edit - tests were conducted in Release build (not debug).


Solution

  • No. Linking a static library is equivalent to linking the object files that comprise the library. There should be no noticable difference at compile/link time nor run-time whether you link in code from a static library or copy-paste the source to files you then build as local .o files.

    Any difference in speed should come from code changes only (although one caveat may be if you use LTO (link time optimization).