News for hackers. Stuff that matters.
Re:Programming Idioms by agent327 on Monday August 16, 2021 @03:52AM (#28195888)
It's still asking for trouble though. Look at this minor variation: 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.

There are no comments on this comment yet.