-1

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?

Sanich
  • 1,739
  • 6
  • 25
  • 43
  • Are you testing this in a real, compiled build with optimisations turned on? Swift playgrounds are not suitable for measuring execution time, since they're not actually compiling the code and the real-time displaying of values also significantly affects execution time. – Dávid Pásztor Aug 04 '20 at 10:02
  • 1
    You can find some examples in [this question](https://stackoverflow.com/questions/25006235/how-to-benchmark-swift-code-execution/25006361#25006361) of measuring performance – Joakim Danielson Aug 04 '20 at 10:36

1 Answers1

2

You could test this in unit tests and use measure block

Please also consider running this on real device, not simulator

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
mkowal87
  • 596
  • 4
  • 19