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

标签 ios objective-c optimization time

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

最佳答案

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/

相关文章:

ios - Segue 不适用于 Swift

ios - 无法使用 AlamoFire 和 SwiftyJSON 解析 JSON

ios - 如何创建带边框的圆形图像(UIGraphics)?

c++ - gcc优化?漏洞?及其对项目的实际意义

mysql - 连接具有多行的表中的一行

ios - 如何可靠地检测 uiview 框架相对于屏幕的原点 Y?

objective-c - MKMetersPerMapPointAtLatitude 背后的数学

iphone - Objective-C 中的 MySQL 命令

java - 在 JavaScript 中创建本地值的方法

android - 在 android 和任何移动应用程序中,为什么它以小尺寸启动?