Search code examples
objective-ccocoaxcodemultithreadingexecution

Why are certain lines in my Cocoa application taking forever to run?


I'm having a very strange problem. I'm running a loop in a detached thread, and some lines of code in my loop (different lines in each iteration of the loop) are taking a long long time (~45 seconds) to execute, even if they are simple commands like initializing an NSXMLDocument with data. This problem is also very inconsistent, and it doesn't occur in the same place or each time. Any ideas why this is happening?

PS. Since my code is so long and complex, and the problem isn't consistent, I can't post any sample code :/


Solution

  • Don't guess about performance issues. There are plenty of tools to help you determine what is going wrong, including the CPU Sampler instrument in the Instruments app, and Shark. Both of these will let you analyse exactly where the CPU time is going, so you can do something about it.