简体   繁体   中英

Measuring execution time with Date()

I want to compare to different implementation of a function:

let start1 = Date()
_ = funcImplA()   
let end1 = Date()

let start2 = Date()
_ = funcImplB()
let end2 = Date()

let time1 = end1.timeIntervalSince(start1)
let time2 = end2.timeIntervalSince(start2)
print("ImplA = \(time1 ), ImplB = \(time2)")

The results I'm getting is that the first measure is always slower than the second one ( time1 > time2 ). Meaning that if I'm switching between the calls, fist measure funcImplB() and then funcImplA , I'm still get that time1 > time2 . What might be the reason?

You could test this in unit tests and use measure block

Please also consider running this on real device, not simulator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM