是否有一种方法可以确定一个方法需要执行多少时间(以毫秒为单位)?
当前回答
我用这个:
clock_t start, end;
double elapsed;
start = clock();
//Start code to time
//End code to time
end = clock();
elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;
NSLog(@"Time: %f",elapsed);
但我不确定iPhone上的CLOCKS_PER_SEC。你还是别打了吧。
其他回答
我知道这是一个旧的,但即使我发现自己又一次徘徊在它旁边,所以我想在这里提交我自己的选择。
最好的办法是看看我的博客文章: 在Objective-C中计时:秒表
基本上,我写了一个类,它以非常基本的方式停止观看,但被封装,所以你只需要做以下事情:
[MMStopwatchARC start:@"My Timer"];
// your work here ...
[MMStopwatchARC stop:@"My Timer"];
最后你会得到:
MyApp[4090:15203] -> Stopwatch: [My Timer] runtime: [0.029]
在日志里…
再一次,看看我的帖子,或者在这里下载: MMStopwatch.zip
我在我的utils库中使用这个(Swift 4.2):
public class PrintTimer {
let start = Date()
let name: String
public init(file: String=#file, line: Int=#line, function: String=#function, name: String?=nil) {
let file = file.split(separator: "/").last!
self.name = name ?? "\(file):\(line) - \(function)"
}
public func done() {
let end = Date()
print("\(self.name) took \((end.timeIntervalSinceReferenceDate - self.start.timeIntervalSinceReferenceDate).roundToSigFigs(5)) s.")
}
}
…然后调用如下方法:
func myFunctionCall() {
let timer = PrintTimer()
// ...
timer.done()
}
…运行后在控制台中看起来是这样的:
MyFile.swift:225 - myFunctionCall() took 1.8623 s.
虽然不像上面的TICK/TOCK那样简洁,但它足以清楚地看到它在做什么,并自动包括正在计时的内容(通过文件、方法开头的行和函数名)。显然,如果我想要更多的细节(例如,如果我不只是计时一个方法调用,因为是通常的情况下,而是在该方法内计时块),我可以在PrintTimer init上添加“name="Foo""参数来命名它除了默认值之外的东西。
一个在Swift 4中使用mach_absolute_time()的细粒度计时的例子:
let start = mach_absolute_time()
// do something
let elapsedMTU = mach_absolute_time() - start
var timebase = mach_timebase_info()
if mach_timebase_info(&timebase) == 0 {
let elapsed = Double(elapsedMTU) * Double(timebase.numer) / Double(timebase.denom)
print("render took \(elapsed)")
}
else {
print("timebase error")
}
这里有另一种方法,在Swift中,使用defer关键字来做到这一点
func methodName() {
let methodStart = Date()
defer {
let executionTime = Date().timeIntervalSince(methodStart)
print("Execution time: \(executionTime)")
}
// do your stuff here
}
来自苹果的文档:defer语句用于在将程序控制权转移到该defer语句出现的范围之外之前执行代码。
这类似于try/finally块,优点是将相关代码分组。
mach_absolute_time()有一个方便的包装器——它是来自CoreAnimation框架的CACurrentMediaTime()函数。
与NSDate或CFAbsoluteTimeGetCurrent()偏移量不同, mach_absolute_time()和CACurrentMediaTime()基于 主机内部时钟,精确,单原子测量,且不受影响 外部时间参考的更改,例如由时间引起的更改 区域、夏令时或闰秒。
ObjC
#import <QuartzCore/QuartzCore.h>
CFTimeInterval startTime = CACurrentMediaTime();
// Do your stuff here
CFTimeInterval endTime = CACurrentMediaTime();
NSLog(@"Total Runtime: %g s", endTime - startTime);
斯威夫特
import QuartzCore
let startTime = CACurrentMediaTime()
// Do your stuff here
let endTime = CACurrentMediaTime()
print("Total Runtime: \(endTime - startTime) s")
推荐文章
- 为什么Swift的编译时间这么慢?
- iPhone上UIView和UILabels的渐变
- keychain上的分发证书中缺少私钥
- 在实现API时,我如何避免在块中捕获自我?
- 如何创建一个Swift Date对象?
- Xcode 4在目标设备上说“finished running <my app>”——什么都没有发生
- 从另一个应用程序打开设置应用程序
- 快速提取正则表达式匹配
- 如何应用梯度的背景视图的iOS Swift应用程序
- 图书馆吗?静态的?动态吗?或框架?另一个项目中的项目
- 如何用SwiftUI调整图像大小?
- Xcode 6 gitignore文件应该包括什么?
- “每个程序员应该知道的关于内存的事情”有多少仍然是有效的?
- 如何在iPhone/iOS上删除电话号码的蓝色样式?
- 检测视网膜显示