0

I want to get the CPU time used by a function in Swift. In C we could do this by clock() function. I read this answer but it measures the real time. Any suggestions?

Community
  • 1
  • 1
bar5um
  • 728
  • 1
  • 8
  • 17

1 Answers1

1

You can use the clock() function from C:

import Foundation
var t = clock()

myLongRunningFunction()

t = clock() - t

print("The function takes \(t) ticks, which is \(Double(t) / Double(CLOCKS_PER_SEC)) seconds of CPU time")
Code Different
  • 82,550
  • 14
  • 135
  • 153