Last active
September 12, 2018 09:34
-
-
Save totiz/1eea62753bd9c1ec4aeef15884671fdd to your computer and use it in GitHub Desktop.
Lazy CPU Performance
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
import UIKit | |
class ViewController: UIViewController { | |
// ตัวอย่างข้อมูลแบบ JSON-Style Data | |
// Count: 50,000 | |
static func fetchDataFromApi() -> [[String: Any]] { | |
var output = [[String: Any]]() | |
for _ in 0..<10000 { | |
output += [["name": "Totiz", "age": 12], | |
["name": "Dog", "age": 12], | |
["name": "Cat", "age": 12], | |
["name": "Book", "age": 12], | |
["name": "Car", "age": 12]] | |
} | |
return output | |
} | |
// ตัวแปรเก็บข้อมูลทดสอบ | |
var data = ViewController.fetchDataFromApi() | |
// หาชื่อ Cat ใน JSON-Style Data | |
// Average Run Time: 0.129 | |
@IBAction func hitMap(sender: AnyObject) { | |
//let startTime = CFAbsoluteTimeGetCurrent() | |
let prepareData = data.map{ $0["name"] as! String } | |
let indexOfCat = prepareData.index { $0 == "Cat" } | |
print( indexOfCat ) | |
//print( CFAbsoluteTimeGetCurrent() - startTime ) | |
} | |
// Lazy Version หาชื่อ Cat ใน JSON-Style Data | |
// Average Run Time: 0.000168 | |
@IBAction func hitLazyMap(sender: AnyObject) { | |
//let startTime = CFAbsoluteTimeGetCurrent() | |
let prepareData = data.lazy.map{ $0["name"] as! String } | |
let indexOfCat = prepareData.index { $0 == "Cat" } | |
print( indexOfCat ) | |
//print( CFAbsoluteTimeGetCurrent() - startTime ) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment