0

I want to get the CPU time used by a function in my app. It works properly on iOS Simulator. but when I run it on my iPhone it just gives 0.

var t = clock()

myLongRunningFunction()

t = clock() - t
bar5um
  • 843
  • 1
  • 9
  • 21

1 Answers1

0

I'm not familiar with the clock() function. Is that a UNIX function?

Terminal has a man page for a clock function but it says that it reports the processor time used in the current process.

I would suggest using the NSDate timeIntervalSinceReferenceDate function instead. That gives a value in seconds and decimal fractions of a second:

var startTime: NSTimeInterval

startTime = NSDate.timeIntervalSinceReferenceDate() myLongRunningFunction() let secondsToRun = NSDate.timeIntervalSinceReferenceDate() - startTime

print(format: "function took %.5f seconds to run", secondsToRun)

Where your result will be expressed in floating-point seconds.

Duncan C
  • 128,072
  • 22
  • 173
  • 272