Skip to content

Instantly share code, notes, and snippets.

@owensd owensd/IterationPerf.swift Secret
Last active Dec 11, 2015

Embed
What would you like to do?
Performance for Iterations In Swift
//
// 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
You can’t perform that action at this time.