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?
Asked
Active
Viewed 1,909 times
0
-
1Swift has `clock()`. Just import `Darwin` (or `Foundation` or `UIKit` which all import `Darwin` eventually). – Rob Napier Oct 15 '15 at 13:37
-
http://stackoverflow.com/questions/2129794/how-to-log-a-methods-execution-time-exactly-in-milliseconds – SoftwareCarpenter Oct 15 '15 at 13:44
1 Answers
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