5 ms·
In real production code, things like this are rarely bottlenecks. The count version, frankly, is less error prone. There are a few different ways that you can e
by rcoh 14y ago
In real production code, things like this are rarely bottlenecks. The count version, frankly, is less error prone. There are a few different ways that you can easily mess up the while loop version. In places where a loop like this really is the bottleneck we'll drop in the C-style version.
In general though, we find the code just kind of balloons if you use patterns like this everywhere.
- jlarocco 14y agoI have to agree with mrdmnd here. I know the old saying about premature optimization, but avoiding premature optimization doesn't mean actively choosing slower code. It just seems like sloppy programming, IMO.
- anorwell 14y agoConcise, readable code should trump performance concerns in cases where performance doesn't matter. The idiomatic version is much shorter and clearer.
- nessus42 14y agoThe equating of writing slower code to "sloppy programming" seems like sloppy reasoning to me. If it was sloppy to knowingly go with a slower solution, then using Python, for instance, or any scripting language for that matter, would always be "sloppy". It isn't. For me, Scala provides a happy middle ground between a scripting language like Python, and an "enterprise" language like Java. Scala is as nearly performant as Java--and sometimes it performs significantly better than Java due to having parallel collections, and the like--while having nearly the expressiveness of a scripting language like Python. Also, Scala's performance issues are overblown. I program in Scala professionally every day and it performs great.