-
-
Save owensd/b438bea90075907fa6ec to your computer and use it in GitHub Desktop.
Performance for Iterations In Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// IterationPerfTests.swift | |
// IterationPerfTests | |
// | |
// Created by David Owens II on 12/11/15. | |
// Copyright © 2015 owensd.io. All rights reserved. | |
// | |
import XCTest | |
@testable import IterationPerf | |
let first = 10000000 | |
let second = 20000000 | |
class IterationPerfTests: XCTestCase { | |
func testZipStride() { | |
self.measureBlock { | |
var sum = 0 | |
for (i, j) in zip(first.stride(to: 0, by: -1), second.stride(to: 0, by: -2)) { | |
if i % 2 == 0 { continue } | |
if j % 3 == 0 { continue } | |
sum += 1 | |
} | |
print(sum) | |
} | |
} | |
func testCStyleFor() { | |
self.measureBlock { | |
var sum = 0 | |
for var i = first, j = second; i > 0 && j > 0; i -= 1, j -= 2 { | |
if i % 2 == 0 { continue } | |
if j % 3 == 0 { continue } | |
sum += 1 | |
} | |
print(sum) | |
} | |
} | |
func testWhileLoop() { | |
self.measureBlock { | |
var sum = 0 | |
if true { | |
var i = first, j = second | |
while i > 0 && j > 0 { | |
defer { | |
i -= 1 | |
j -= 2 | |
} | |
if i % 2 == 0 { continue } | |
if j % 3 == 0 { continue } | |
sum += 1 | |
} | |
} | |
print(sum) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment