News for hackers. Stuff that matters.
Re:strcpy: A niche function you don't need by mlindner on Friday July 30, 2021 @08:41PM (#28014121)
It's also slowest of all the string functions. Additionally, providing the NULL as the buffer argument does all the work of creating the actual string you're going to copy (in the case of using multiple format characters) and then discarding it. So you have to do the format operation twice, doubling the cost of any string operation.

Oh and it's not compatible with UTF-8.

Re:strcpy: A niche function you don't need by fanf2 on Friday July 30, 2021 @08:50PM (#28014173)
What makes it incompatible with UTF-8?
Re:strcpy: A niche function you don't need by colejohnson66 on Friday July 30, 2021 @09:54PM (#28014516)
Normally, UTF-8 works with C strings no problem, but it's possible that a multi-byte UTF-8 encoded codepoint is only halfway copied when it reaches the capacity and throws the NULL at the end.
Re:strcpy: A niche function you don't need by x4e on Saturday July 31, 2021 @03:10AM (#28016066)
Why is snprintf slow? I am surprised that it would be slow especially when compared to methods like asprintf that allocate the buffer.
Re:strcpy: A niche function you don't need by mlindner on Saturday July 31, 2021 @03:32AM (#28016174)
I forget the exact reasoning now, but I remember it being about 10x slower than memcpy or strncpy. I think the main reason was because of the need to parse the format string.
Re:strcpy: A niche function you don't need by formerly_proven on Saturday July 31, 2021 @04:05AM (#28016310)
Even then, printf and scanf are typically faster (and not even by a little bit, by a lot) than C++ iostreams formatted output, even though iostreams gets all the formatting information at compile-time, while printf has to parse the format string.

On the other hand, if people start to use snprintf in that particular form as a safe way of string copying, compilers could pattern-match this and substitute a direct implementation.

Re:strcpy: A niche function you don't need by quietbritishjim on Saturday July 31, 2021 @12:11PM (#28019371)
The modern C++ way of formatting strings is with std::format, or the external fmt library. It's faster than printf (and certainly streams!) while having convenient format strings (unlike streams) and optional compile time format parsing, combined with c++ type safety.
Re:strcpy: A niche function you don't need by pjmlp on Saturday July 31, 2021 @12:51PM (#28019750)
Everyone complains about streams, yet since Turbo C++ 1.0 for MS-DOS they have served me well for the kinds of applications I delivered into production with C++.
Re:strcpy: A niche function you don't need by josefx on Saturday July 31, 2021 @04:11AM (#28016349)
Isn't the biggest iostream bottleneck the forced sync with cstdio? You can turn that of at program start.
Re:strcpy: A niche function you don't need by formerly_proven on Saturday July 31, 2021 @05:51AM (#28016768)
With that enabled it's comically slow, but even without the stdio syncing (and even operating on stringstreams) it's still much slower. For simple one-off formatting it's hard to notice, but once you write a parser for some engineering file formats (most of which are still ASCII) and have to read a few million floats here or there it becomes a really stark difference. For parsing floats specifically there are some implementations around which are optimized more, though some of the "fast and clever" ones are less accurate.
Re:strcpy: A niche function you don't need by x4e on Saturday July 31, 2021 @03:51AM (#28016248)
Oh I see, I thought you were comparing it to the printf style methods but compared to methods that do not take a format string that makes sense.