gpt4 book ai didi

ios - 如何以毫秒为单位准确记录方法的执行时间?

转载 作者:IT老高 更新时间:2023-10-28 12:14:55 26 4
gpt4 key购买 nike

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

最佳答案

NSDate *methodStart = [NSDate date];

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

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

swift :

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com