Mar 3, 2022

[POSIX][C][C++] snprintf / sprintf can be thread safe thus slow

Reference:
https://aras-p.info/blog/2022/02/25/Curious-lack-of-sprintf-scaling
https://developer.arm.com/documentation/dui0492/i/the-c-and-c---libraries/thread-safe-c-library-functions
https://twitter.com/aras_p/status/1496489672373063682

snprintf / sprintf use global locale; which the function itself is thread-safe; thus slow and not scale(contains mutex)

take away:

  1. for converting int to string, use std::to_chars instead of snprintf
  2. {fmt} comes to the rescue (increase compile time)
  3. For using Double-checked_locking, be ware: use atomic with memory model, don't use simple bool/int to make it cross platform.
  4. don't use iostream for high-performance code
  5. always profiling.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.