Created
June 2, 2022 11:19
-
-
Save ioquatix/e58cb756c4072fde7ae7961a5be08d56 to your computer and use it in GitHub Desktop.
UIImage 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
extension DispatchTimeInterval { | |
func toDouble() -> Double? { | |
var result: Double? = 0 | |
switch self { | |
case .seconds(let value): | |
result = Double(value) | |
case .milliseconds(let value): | |
result = Double(value)*0.001 | |
case .microseconds(let value): | |
result = Double(value)*0.000001 | |
case .nanoseconds(let value): | |
result = Double(value)*0.000000001 | |
case .never: | |
result = nil | |
} | |
return result | |
} | |
} | |
import UIKit | |
let path = Bundle.main.path(forResource: "dog", ofType: "png")! | |
let startTime = DispatchTime.now() | |
// On my laptop, takes around 120ms. | |
let image = UIKit.UIImage.init(contentsOfFile: path)! | |
let endTime = DispatchTime.now() | |
image.size | |
let duration = startTime.distance(to: endTime).toDouble()! | |
print("Time to evaluate: \(duration*1000.0)ms") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment