Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Workaround for poor multithreaded performance of localtime_s() on Windows

It seems like localtime_s() (which is the equivalent of the standard localtime_r) contains a critical section in MSVC.

For comparison, here are 2 sample apps, one does localtime_s in a loop, the other gmtime_s.

  • http://rextester.com/OQJ48177
  • http://rextester.com/JNDR45936

Profiling shows heavy lock contention inside isindst called from common_localtime_s<__int64>:

localtime lock contention

gmtime does not exhibit the issue:

gmtime no lock contention

Is there any way to work around this to get sane localtime_s performance in a multithreaded environment, provided I do need local times in my process?

like image 211
rustyx Avatar asked Mar 02 '16 14:03

rustyx


1 Answers

Here is one proposed solution:

Record all times in whatever is the fastest. When presenting them to a user through a GUI, a log file, or whatever, do the conversion to local time then.

Since most GUIs and log output are single threaded, this should remove the contention from the rest of the program.

If the program is never presenting data to a user, then just write it out in the fast time format and use a post-processing tool to convert it or display it.

like image 147
Zan Lynx Avatar answered Oct 27 '22 01:10

Zan Lynx