4 ms·
> C has always been the fastest high-level language. C has another big speed problem. Strings are 0 terminated, rather than length terminated. This means const
by WalterBright 2y ago
> C has always been the fastest high-level language.
C has another big speed problem. Strings are 0 terminated, rather than length terminated. This means constant scanning of strings to find their length. Even worse, the scanning of the string reloads the cache with the string contents, which is pretty bad for performance.
Of course, you could use `struct String { char *p; size_t length; };` but since every library you want to connect to uses 0 terminated strings, you're out on your island all alone, so pragmatically it does not work.
Another speed-destroying problem with C strings is you cannot take a substring that does not require allocating a new string and then copying the data. (Unless the substring is right-justified.) This is not fast in any universe.
D uses length-denoted strings as a basic data type, and with string processing code, it is much faster than C. Substrings are quick and easy. You can still interface with C because D string literals implicitly convert to C string literals, as the literals are 0 terminated. So this works in D:
printf("hello world!\n");
(People sometimes rag on me for still using printf, but printf is the most optimized and debugged library function in the world, so I take advantage!)
- pizlonator 2y agoYeah totally. It's a perfect example of C being optimized for simple mapping onto linear memory, rather than some kind of performance optimum
- Dylan16807 2y agoI don't know, a pointer with a size is also quite simple.