←back to thread

3883 points kuroguro | 1 comments | | HN request time: 0.217s | source
Show context
z92 ◴[] No.26296975[source]
Also note that the way he fixed it, strlen only caches the last call and returns quickly on an immediate second call with the same string.

Another reason why C styled null terminated strings suck. Use a class or structure and store both the string pointer and its length.

I have seen other programs where strlen was gobbling up 95% of execution time.

replies(3): >>26297107 #>>26299247 #>>26299304 #
1. ziml77 ◴[] No.26299247[source]
I'm with you. I hate null terminated strings. In my first experiences with C, I specifically hated them because of strlen needing to scan them fully. When C++ introduced string_view, my hate grew when I realized that I could have zero-copy slices all the way up until I needed to interface with a C API. At that point you're forced to copy, even if your string_view came from something that was null terminated!