I noticed a big difference in performance of my C program depending on the -fPIC flag. When I use it my program is about 30% slower than without it. I am comparing it with a Lua program which calls a C function (where all the heavy calculation is done). Firstly I created a shared object with the C function, so had to use the -fPIC flag. The performance is very similar to the C code with -fPIC flag. So now I tried to the same without the .so: I called Lua from C:
int main()
{
lua_State* L = luaL_newstate();
luaL_openlibs(L);
lua_register(L, "my_c_function", my_c_function);
luaL_dofile(L, "my_lua_program.lua");
lua_close(L);
return 0;
}
But here performance is the same regardless if I use the -fPIC flag or not (and the same as the approach with .so). I was expecting some improvement without the -fPIC flag... Any advice on how can I investigate it further? Is the second approach creating position independent code anyway and that's why the performance is similar? Thanks!
More information, as suggested by the comment: I use the -O3 flag, gcc 4.7.2, Ubuntu 12.04.2, x86_64. Yes, I was quite surprised with so big overhead... My program is calculating Mandelbrot fractal. So there are two loops iterating over x and y and the function I have in C is isMandelbrot: it takes the number of iterations and returns bool: belongs to Mandelbrot set or not. I use the shared object with 'require'.