memC
memC

Reputation: 1025

which one is recommended: using static lib vs dynamic lib (shared object)

I am working with an application in Linux. It supports both static and dynamic (.so) versions

From the performance standpoint, which version should a user use? The application performs computational tasks that require several hours of CPU time.

Any other advantage of using one lib over the other?

Thanks

Upvotes: 4

Views: 826

Answers (4)

Artyom
Artyom

Reputation: 31263

From pure performance point of view:

Shared Objects are compiled as PIC (position independent code) that theoretically may be slightly less efficient then normal code on some architectures (including x86).

However, I don't think this would make any real difference.

From any other points

Use shared object, it has too many advantages over static library that it is just better alternative.

Upvotes: 4

antik
antik

Reputation: 5330

From a performance standpoint, the differences are minimal unless the dynamic library would be loaded and unloaded often.

The only difference is that a dynamic library is loaded when needed instead of being built into (and thus ever present, sans load time) your executable.

The dynamic library can also be reused by several executables. This is the main reason I've used dynamic libraries in the past.

Upvotes: 3

user253984
user253984

Reputation:

Usually statically bound libraries are faster as they do not have the overhead of locating and loading of the library, but: for a multi-hour program the performance difference should be too small to notice.

Anyways, the only way to be really sure is: Benchmark it yourself.

Upvotes: 1

user231967
user231967

Reputation: 1975

Normally you'd use the dynamic lib to reduce the size of the binary. No penalty at runtime except for the startup of the application which probably doesn't matter.

Upvotes: 2

Related Questions