如何以毫秒为单位准确记录方法的执行时间? [英] How to log a method's execution time exactly in milliseconds?

查看:21
本文介绍了如何以毫秒为单位准确记录方法的执行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to determine how much time a method needs to execute (in milliseconds)?

推荐答案

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)")

易于使用且具有亚毫秒级精度.

Easy to use and has sub-millisecond precision.

这篇关于如何以毫秒为单位准确记录方法的执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆