effect of -fPIC on code speed

Andrew Haley aph@redhat.com
Thu Sep 16 11:23:00 GMT 2010


On 09/16/2010 11:11 AM, Miles Bader wrote:
> I recently wanted to produce both a standalone executable and a shared
> library using the same set of object files, and so compiled them all
> using -fPIC (as "gcc -shared" demeands it).  I made my shared lib, and
> also made a normal executable using these object files -- and I
> noticed that the resulting executable size was a fair bit smaller
> (according to the "size" command) than the previous non-fPIC
> executable I had compiled (using non-fPIC object files).
> 
> I haven't done any significant amount of benchmarking on it, but it
> didn't seem obviously slower.
> 
> [This is on an x86-64 system, using a g++-snapshot from debian:
> gcc (Debian 20100828-1) 4.6.0 20100828 (experimental) [trunk revision 163616] ]
> 
> Is there any general wisdom about the effects of using -fPIC,
> especially on code speed?  Would it be stupid for me to set up my
> configure script to _always_ use -fPIC (even when not needed for a
> shared library), when it detects that gcc accepts that option?

I don't think it will do much harm, but I don't think it will do much
good either.  There will be some size increase with PIC, and that will
inevitably cause some slowdown.  Some processors are better than others:
32-bit x86 isn't so good with PIC.  In the main, though, it's only a
second-order effect.

Andrew.



More information about the Gcc-help mailing list