I need the PAGESIZE
value each time my function is called, it's looks like caching system call sysconf(_SC_PAGESIZE)
in a function static variable is much faster than calling it every time.
man page says:
The values obtained from these functions are system configuration constants. They do not change during the lifetime of a process.
Then, why successive calls to sysconf are so slow if value does not change ?
Here is the code I used to test:
#include <unistd.h>
long a()
{
return sysconf(_SC_PAGESIZE);
}
long b()
{
static long n;
if (!n)
n = sysconf(_SC_PAGESIZE);
return n;
}
int main()
{
for (int i = 800000000; i > 0; --i)
a();
return 0;
}
Result are: 4,8s for a() vs 1,7s for b(). It is even worse with -O2
.