0

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).

12
  • 3
    "While debugging the new software..." you weren't inferring performance from a debug build were you? Performance: release build (optimised) and profile it. Commented Feb 1, 2018 at 18:57
  • Lolx! Great point. I can't see why there wold be any difference - in both cases, it is linked into the same executable. I can only suggest profiling. Commented Feb 1, 2018 at 18:58
  • @RichardCritten No. Great question though. I should have made it clear that my tests were all in release build. Same config as I would use to distribute. Commented Feb 1, 2018 at 18:58
  • "The speed increase which resulted " - how did you measure that? Commented Feb 1, 2018 at 18:59
  • @Mawg Software timers built into the code. Commented Feb 1, 2018 at 19:00

1 Answer 1

4

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).

Sign up to request clarification or add additional context in comments.

3 Comments

This is what I assumed, so I'm glad to have the confirmation. Your last sentence raises a bit of doubt though. Could it be that the linker (or compiler, for that matter) would optimize better if all of the code was built together? Isn't link-time optimization common for release builds?
@Blair Fonville Yes, it can sometimes optimize better, but LTO is usually something you need to explicitly enable.
I'm using VS-2012 for this. Optimization is set to "Use Link Time Code Generation (/LTCG)". I'll have to see if I can find some info on that. Thanks for the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.