[英]CPU usage in iPad Simulator
I am puzzled by a test involving iPad simulator. 我对涉及iPad模拟器的测试感到困惑。 The only difference is the placement of NSDateFormatter
. 唯一的区别是NSDateFormatter
的位置。
NSDateFormatter
initialization code placed in a dispatch queue. NSDateFormatter
初始化代码放在一个调度队列中。 This means the initialization is performed every time the dispatch queue is called. 这意味着每次调用调度队列时都会执行初始化。
Output: 输出:
iPad Simulator CPU = 106% (constant)
NSDateFormatter
initialization code placed in a viewDidLoad
method. NSDateFormatter
初始化代码放在viewDidLoad
方法中。 This means the initialization is initialized once. 这意味着初始化初始化一次。
Output: 输出:
iPad Simulator CPU = 133% (fluctuates +/- 2%)
Any ideas? 有任何想法吗?
Memory allocation is the single most expensive non-disk operation. 内存分配是最昂贵的非磁盘操作。 If you are running short blocks from a dispatch queue, each block is spending most if it's time allocating a date formatter. 如果从调度队列中运行短块,则每个块在花时间分配日期格式化器时花费最多。
By moving the date formatter initialization into viewDidLoad, you create one and only one date formatter and reuse it. 通过将日期格式化程序初始化移动到viewDidLoad,您可以创建一个且只有一个日期格式化程序并重复使用它。
The lesson: Don't do memory allocations in oft-repeated code if you can help it. 经验教训:如果可以提供帮助,请不要在经常重复的代码中进行内存分配。
Don't check performance in the simulator. 不要检查模拟器中的性能。 Only test CPU usage on a device. 仅测试设备上的CPU使用情况。 The simulator is running on your computer's intel CPU. 模拟器在您的计算机的intel CPU上运行。 Always do performance tests on a real device. 始终在真实设备上进行性能测试。 You might be looking at a bug or poor implementation in the simulator. 您可能正在查看模拟器中的错误或不良实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.