4 ms·
If we are being cryptic already, why not int num_empty_strings = !!*s1 + !!*s2 + !!*s3;
by stkdump 3y ago
If we are being cryptic already, why not
int num_empty_strings = !!*s1 + !!*s2 + !!*s3;
- gjm11 3y agoThat isn't only more cryptic, it's also potentially a lot more efficient -- strlen takes time proportional to the length of the string, which of course you don't need to do if you only care whether or not the length is zero. You shouldn't use strlen for empty-string tests.
- LegionMammal978 3y agoIn practice, GCC and Clang don't seem to have any issues inlining the necessary part of strlen at -O1 or higher (https://godbolt.org/z/rM198aYea https://godbolt.org/z/rM198aYea). But MSVC inlines the empty-string case, while still calling out for nonempty strings, probably since it doesn't realize that the returned length will be nonzero.
- stkdump 3y agoI guess since strlen uses an unsigned size, which has specified overflow behavior the compiler not only has to proof the initial iteration, but also all the ULLONG_MAX+1 multiples, which of course refer to the same memory address. But maybe its harder for the optimizer to see.