Skip to content

Instantly share code, notes, and snippets.

@owensd
Last active December 11, 2015 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save owensd/b438bea90075907fa6ec to your computer and use it in GitHub Desktop.
Save owensd/b438bea90075907fa6ec to your computer and use it in GitHub Desktop.
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