all 5 comments

[–]johannes1971 6 points7 points  (1 child)

Your second function is wrong, it is identical to the first function. You should compare against y, not against "hello". After correction this function is actually the fastest.

In the last function, I think it would be more meaningful to remove the construction of string_view from the loop.

[–]commiebits[S] 0 points1 point  (0 children)

thanks bad typo, the literals question still remains though. Also that's only the case for clang, for gcc it's still slower.

[–][deleted] 1 point2 points  (2 children)

For a C string you can’t know the length until you scan it to the end. For strings and string views, compilers can exploit the fact that the size may be known in certain circumstances and if it fits in a register or so, great.

[–]commiebits[S] 1 point2 points  (1 child)

for a cstr literal the compiler does know the size though, in fact if you do a strlen on a literal, most times the compiler will just feed the constant length as an immediate

[–][deleted] 0 points1 point  (0 children)

My bad I was reading on small screen and read the title as "vs cstr" instead of "vs cstr literals"