为什么第一次运行"XCTestCase -measureBlock:"?需要这么多时间? [英] Why does the first run of "XCTestCase -measureBlock:" takes so much time?

查看:221
本文介绍了为什么第一次运行"XCTestCase -measureBlock:"?需要这么多时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xcode7.iPhone5s模拟器(iOS 9.0).

I'm using Xcode 7. iPhone 5s simulator (iOS 9.0).

我正在尝试使用XCTestCase -measureBlock:来衡量我在某些功能上花费的时间.这会将代码在代码块中运行10次,并报告结果,每次运行需要多长时间,平均值,STDEV等.

I'm trying to use XCTestCase -measureBlock: to measure time spent on some of my functions. This runs the code inside the block 10 times and report the results how long each run takes, the average, STDEV, etc.

事实证明,第一次运行的时间总是很长,例如要高出400-500%.对于非常简单的方法,甚至在测量块中什么也没有发生.我的- (void)setUp- (void)tearDown中没有任何东西.

It turns out that the time of first run is always very high, like 400-500% higher. Same happens for a very simple method, or even nothing in the measure block. I don't have anything in my - (void)setUp or - (void)tearDown.

NSString -stringWithFormat:的结果.请参阅图表第一栏的峰值. values: [0.000031, 0.000005, 0.000003, 0.000003, 0.000003, 0.000003, 0.000003, 0.000003, 0.000003, 0.000003]

Result of NSString -stringWithFormat:. See the spike at the first bar of the graph. values: [0.000031, 0.000005, 0.000003, 0.000003, 0.000003, 0.000003, 0.000003, 0.000003, 0.000003, 0.000003]

空白块的结果.相似的结果. values: [0.000007, 0.000001, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000001]

Result of a blank block. Similar results. values: [0.000007, 0.000001, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000001]

这是错误还是什么?发生了什么事?

Is this a bug or something? What is going on?

推荐答案

这可能是某些缓存升温的产物.

It's probably an artifact of some caches warming up.

例如,Objective-C运行时为每个类维护选择器缓存.这意味着对每个方法的第一次调用比随后的调用要慢一些.

The Objective-C runtime maintains a selector cache for each class, for example. This means that the first call to each method is somewhat slower than the following calls.

您可以将被测块的内容恰好放在调用measureBlock:之前,以测试是否为真.因此,预热运行将无法进行测量.

You could just put the contents of the measured block right before the call to measureBlock: to test if this is true. So the warm up run would just not be measured, then.

这篇关于为什么第一次运行"XCTestCase -measureBlock:"?需要这么多时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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