4 ms·
Formatting is a natural demarcation point for deferring to another thread as it can be surprisingly expensive. You can create a workqueue where each item is a
by CoolGuySteve 6y ago
Formatting is a natural demarcation point for deferring to another thread as it can be surprisingly expensive.
You can create a workqueue where each item is a copy of the argument list and a const char* of the format string. Then your program will only take a microsecond or two to print (depending on the argument length) on the critical path.
- klodolph 6y agoFormatting is generally fast enough in practice that synchronizing with another thread would slow things down. So it is actually not a good place to offload work to other threads, since you spend a similar amount of work just sending the message. Unless you are formatting a really large message.
- CoolGuySteve 6y agoYou should time it and come back. You're wrong.
- nkurz 6y agoYou might be right, but your response comes across as rather rude. I think you should either soften your message, or provide more details about the timing --- or both! My instinct is that for standard IO involving moderate amounts of string interpolation and number formatting in C running on Linux on modern Intel processors, klodolph is probably closer to right. But maybe you are right for some other setup? Or maybe klodolph and I are both wrong, in which case some more details would be helpful.
- CoolGuySteve 6y agoI gave a time in my original post: 1-2usec. Printing a handful of doubles with a cold instruction cache using the standard stdio headers takes longer than that due to all the special casing. klodolph made some handwavey claim about synchronization overhead that are clearly false. It doesn't even make sense to me since the device being printed to will need some sort of synchronization and syscall. Both you and klodlph are also making handwavey claims about formatting speed in Linux. You both need to measure before you argue. I find it extremely rude to argue from a position of ignorance as both of you are doing. "Well gee, I dunno but you're probably wrong". It's so asinine.
- klodolph 6y agoSure, if you have a cold instruction cache. If stdio is not in your instruction cache, or at least in L2, then you’re not using it very much, and you won’t get much benefit from moving it to a different thread. My measurements put stdio formatting at around 100ns for the typical messages I’m printing. So, offloading it to another thread is a fairly clear loss. Even for floating point numbers, you’ll hit the fast path with fairly high regularity. My measurements put it at around 80ns for a double, on my system, most of the time.