[swift-evolution] [Review] SE-0007 Remove C-style for-loops with conditions and incrementers

Paul Cantrell cantrell at pobox.com
Thu Dec 10 16:41:23 CST 2015


> Is there any guarantee that these two loops have the exact same runtime performance?
> 
> for (i, j) in zip(10.stride(to: 0, by: -1), 20.stride(to: 0, by: -2)) {
>    if i % 2 == 0 { continue }
>    print(i, j)
> }
> 
> for var i = 10, j = 20; i > 0 && j > 0; i -= 1, j -= 2 {
>    if i % 2 == 0 { continue }
>    print(i, j)
> }


In a quick and dirty test, the second is approximately 34% slower.

I’d say that’s more than acceptable for the readability gain. If you’re in that rare stretch of critical code where the extra 34% actually matters, write it using a while loop instead.

P



> On Dec 10, 2015, at 4:07 PM, David Owens II via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
>> On Dec 10, 2015, at 1:57 PM, thorsten--- via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> Yes, performance is one thing neglected by the discussions and the proposal.
> 
> This is my primary objection to to this proposal; it assumes (or neglects?) that all of the types used can magically be inlined to nothing but the imperative code. This isn’t magical, someone has to implement the optimizations to do this.
> 
> Is there any guarantee that these two loops have the exact same runtime performance?
> 
> for (i, j) in zip(10.stride(to: 0, by: -1), 20.stride(to: 0, by: -2)) {
>    if i % 2 == 0 { continue }
>    print(i, j)
> }
> 
> for var i = 10, j = 20; i > 0 && j > 0; i -= 1, j -= 2 {
>    if i % 2 == 0 { continue }
>    print(i, j)
> }
> 
> 
> And can you guarantee that performance is relatively the same across debug and release builds? Because historically, Swift has suffered greatly in this regard with respects to the performance of optimized versus non-optimized builds.
> 
> These types of optimizer issues are real-world things I’ve had to deal with (and have written up many blog posts about). I get the desire to simplify the constructs, but we need an escape hatch to write performant code when the optimizer isn’t up to the job.
> 
> -David
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151210/75daa9de/attachment.html>


More information about the swift-evolution mailing list