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
.
Profiling shows heavy lock contention inside isindst
called from common_localtime_s<__int64>
:
gmtime
does not exhibit the issue:
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With