4 ms·
It's still asking for trouble though. Look at this minor variation: https://godbolt.org/z/ncjEejsxe https://godbolt.org/z/ncjEejsxe It shows what happens if yo
by agent327 5y ago
It's still asking for trouble though. Look at this minor variation: https://godbolt.org/z/ncjEejsxe https://godbolt.org/z/ncjEejsxe
It shows what happens if you take away the compilers' ability to reason about 's': it could be a global variable, and 'foo' could be modifying it, so now the compiler has to call strlen on every iteration.
So even though 'strlen' gets optimized out in the original version, it's quite a maintenance hazard since any minor change could inadvertently change the complexity from linear to quadratic. It also doesn't get optimized in debug builds, making those far slower than necessary.