Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top