Skip to content

Instantly share code, notes, and snippets.

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