Created
October 4, 2018 07:42
-
-
Save AvdLee/31f52bf77ddccf8c02defbd727366eba to your computer and use it in GitHub Desktop.
For each vs for loop performance
This file contains hidden or 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
//: Playground - noun: a place where people can play | |
import XCTest | |
class MyTests: XCTestCase { | |
lazy var testData: [Int] = { | |
return (0..<1000).map { Int($0) } | |
}() | |
func testForLoop() { | |
measure { | |
var evenNumbers: [Int] = [] | |
for number in testData { | |
if number % 2 == 0 { | |
evenNumbers.append(number) | |
} | |
} | |
} | |
} | |
func testForEachLoop() { | |
measure { | |
var evenNumbers: [Int] = [] | |
testData.filter { number in number % 2 == 0}.forEach { evenNumbers.append($0) } | |
} | |
} | |
} | |
MyTests.defaultTestSuite.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment