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
  • 843
  • 1
  • 9
  • 21

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
  • 90,614
  • 16
  • 144
  • 163