Performance strftime vs snprintf

I came across an interesting performance puzzle, but before I start to understand glibc and enter the errors left to the right and center, I just wanted to get an idea of ​​what might be there.

I have code that in one of the functions does this:

gettimeofday( &tv, 0);
localtime_r( &tv.tv_sec, &local_tm );
char result[25];
strftime( result, 24, "%Y-%m-%d %H:%M:%S", &local_tm);

The rest of the code does not matter for this question. When I replace it with this:

gettimeofday( &tv, 0);
localtime_r( &tv.tv_sec, &local_tm );
char result[25];
snprintf(result, sizeof(result), "%04d-%02d-%02d %02d:%02d:%02d",
         local_tm.tm_year+1900, local_tm.tm_mon+1,
         local_tm.tm_mday, local_tm.tm_hour, local_tm.tm_min,
         local_tm.tm_sec);

on average, I get a performance improvement of 20%.

Has anyone come across this? Is this OS specific?

+5
source share
1 answer

POSIX strftime tzset() ( , ), linux, , /etc/timezone , ( snprintf). TZ, , .

, .

+6

All Articles