Performance difference between static and shared library

Asked

Viewed 264 times

4

Which is the best performance? Compile the program using libraries such as Mysql Connector and Sqlite as Static (staying inside the compiled binary) or as Shared being separated from binary.

In addition, it is possible to use a shared library in a subfolder instead of standing together with the binary executable?

1 answer

4


On load the dynamic library will have to assemble a table where the symbols are available that the code will access. The difference is very small and will not make any important difference. There may be gain in the library load that may already be in memory. As it can save memory consumption, there may be gain by having more chance of being cached.

There may be a overhead in function calls, as this test demonstrates.

In applications that use databases, where minimal access is absurdly more expensive than the executable load, it will not affect anything significantly.

It is possible in some situations to get better performance because of better optimization to compile statically. And there may be some gain for the reference location. Don’t expect miracles, or make a difference in most applications.

I particularly prefer static compilation whenever possible.

It is possible to place compiled libraries in folders where the system is instructed to search by default or that the application knows in some way that it should search in these locations. This is usually a headache and today one avoids a lot. We are no longer in time that this is an advantage.

It is better to leave the dynamic libraries when it is something that the operating system already has without ambiguity or if it will create a system of plugin/hot patch.

This can be useful.

More details on the subject at What is the difference between static and dynamic linkage?

  • I really liked your comment and the references, but there’s one thing that doesn’t go out of my mind, an application using dynamic libraries when it runs it will load everything into memory? Does static do that too? Because how am I developing a full C++ site wouldn’t it load all pages into memory? Thanks again.

  • 1

    Well this is already another question, but in general the operating system only carries in memory what is necessary, ie load the table if symbols and what is being used.

  • Thank you so much for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.