有没有一种方法可以确定一个方法需要执行多少时间(以毫秒为单位)?

最佳答案

NSDate *methodStart = [NSDate date];

/* ... Do whatever you need to do ... */

NSDate *methodFinish = [NSDate date];
NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];
NSLog(@"executionTime = %f", executionTime);

雨燕:
let methodStart = NSDate()

/* ... Do whatever you need to do ... */

let methodFinish = NSDate()
let executionTime = methodFinish.timeIntervalSinceDate(methodStart)
print("Execution time: \(executionTime)")

Swift3:
let methodStart = Date()

/* ... Do whatever you need to do ... */

let methodFinish = Date()
let executionTime = methodFinish.timeIntervalSince(methodStart)
print("Execution time: \(executionTime)")

易于使用,并具有亚毫秒级的精度。

关于ios - 如何准确记录方法的执行时间(以毫秒为单位)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2129794/

10-09 02:53