-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBenchmark.swift
67 lines (43 loc) · 961 Bytes
/
Benchmark.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import Foundation
public extension Optional {
public func ifSome(@noescape f: (Wrapped) throws -> Void) rethrows {
if case .Some(let unwrapped) = self {
try f(unwrapped)
}
}
public func ifNone(@noescape f: Void throws -> Void) rethrows {
if case .None = self {
try f()
}
}
}
let testCount = 1_00_000_000
var array = [Int?]()
array.reserveCapacity(testCount)
for i in 0..<testCount {
if i % 2 == 0 {
array.append(nil)
} else {
array.append(i)
}
}
var sum = 0
var start = NSDate()
for i in array {
i.ifSome {
sum = sum + $0
}
}
var end = NSDate()
var interval = end.timeIntervalSinceDate(start)
print("Duration ifSome: \(interval)")
sum = 0
start = NSDate()
for i in array {
if let i = i {
sum = sum + i
}
}
end = NSDate()
interval = end.timeIntervalSinceDate(start)
print("Duration if let: \(interval)")