Skip to content

Instantly share code, notes, and snippets.

@totiz
Last active September 12, 2018 09:34
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 totiz/1eea62753bd9c1ec4aeef15884671fdd to your computer and use it in GitHub Desktop.
Save totiz/1eea62753bd9c1ec4aeef15884671fdd to your computer and use it in GitHub Desktop.
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