Don't check performance in the simulator. Only test CPU usage on a device. The simulator is running on your computer's intel CPU. Always do performance tests on a real device. You might be looking at a bug or poor implementation in the simulator.
CPU usage in iPad Simulator
-
16-07-2023 - |
Question
I am puzzled by a test involving iPad simulator. The only difference is the placement of NSDateFormatter
.
First Scenario
NSDateFormatter
initialization code placed in a dispatch queue. This means the initialization is performed every time the dispatch queue is called.
Output:
iPad Simulator CPU = 106% (constant)
Second Scenario
NSDateFormatter
initialization code placed in a viewDidLoad
method. This means the initialization is initialized once.
Output:
iPad Simulator CPU = 133% (fluctuates +/- 2%)
Any ideas?
La solution
Autres conseils
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.
The lesson: Don't do memory allocations in oft-repeated code if you can help it.