Improving iOS performance with Xcode and Instruments for dummies
Hi friend, is your app running a bit slow? Are you, perhaps, unsure what to do about it? Where do you start to investigate? Perhaps, like me, you started searching 🕵️️ in your code for something evident? (Only to realize that you are going down a never ending rabbit hole?)
Well, stress not my friend because Xcode got you covered ✅ and it will help you find your issues (or put you on the right track at least).
This is a second article that I am writing about an Integrated Development Environment (IDE). The more that I use Xcode, the more I like it 😎 Xcode is really, really awesome. Just give it a try and use it.
Side note: it only took me 182 hours to get to the Instruments panel 😅
I’m actually really surprised at the amount of tooling that is given to you in this wonderful IDE. My software engineering training gave me my introduction to IDEs via Eclipse and NetBeans. I’ve spent years using those tools (and don’t get me wrong, they are powerful). But to be completely honest, to this day I will pick anything else but these two. They are ugly, clunky, and not intuitive 🌶
Why did I type that paragraph above?
Because if you have an aversion to IDEs and think that Xcode looks just like the other IDEs, then I am here to tell you that it really is not. Xcode *IS* a pleasure to use and I use it every day. (PS: If you are getting angry while reading this and saying **!?Xcode sucks!?** Don’t get so upset. I will admit, it is not perfect.)
Instruments is this powerful tool, built on top of DTrace, that comes with Xcode. There is no need to install additional software. Just download Xcode once and you have everything you need. Don’t have internet connection or you don’t want to be connected? No worries, you can still DO your work.
The following text is basically my brief overview of Instruments.
When you open Instruments, it looks like this:
You can launch Instruments like so:
Or like this:
(I didn’t want to leave you waiting while my slow computer takes its time to compile and open up… but you get the idea)
What you do next is kind of interesting. All of the icons that you see above, in the main screen of Instruments, represent the different kinds of utility that you can run on your application. (I think of this main screen as a collection of apps for your app). Hopefully you will understand why I say on your application.
Out of all of the available options, I’ve only gotten familiar with Activity Monitor, Allocations, Leaks, and Time Profiler. Oh yes, not only are these tools wonderful but the naming is also intuitive and they are grouped in functionality by colour!
- Activity Monitor and Time Profiler are blue and they monitor 🔎 CPU stuff
- Allocations and Leaks are orange and they monitor 🔎 memory stuff. (Guess what the orange Zombies thing looks at? Did you guess RAM? 😎)
You might be wondering to yourself… OK, all of these things are cool sounding, so what? What do I do with them? Well the cool thing about utility these things is that you can just JUMP in and you will figure things out as you go. It is that easy.
I suggest that you play with the other tools available. I am going to focus on the Time Profiler for the rest of this article.
Time Profiler — used for…profiling… time?
Basically, I open it up and it looks like this:
How about we click on that big, red, record button?
It will look like this:
Now, just use your app as you normally would! Let Time Profiler do its thing. What Instruments, or should I say Time Profiler, does in the background is — it samples your environment while you use your app. In other words, Time Profiler records what it sees your app is doing from the time you pressed record and until you press stop. Makes sense? (if you have ever used software that has some kind of timeline manipulation, then you will be just fine)
So this is a snapshot of how your app behaves, or its environment:
Time Profiler records many such samples at a regular interval over time …
… until you press the stop button of course ✋
Now you get to take off 🔝 your developer hat 🎩 and play a detective 🕵️ … if you want to of course…
Since we are exploring the Timing Profiler, we are interesting in timing. Go ahead and expand ⏬ the tree that Xcode conveniently made for you. Go exploring 🗺. Also, if you notice as you expand the tree, the timing gets more specific (2.20 s, to 1.55s, and so on…) See which areas or which methods in your code take a long time. It very simple.
Just click expand ⏬ and dive 🏊 in:
Don’t know where to start? Try this:
- Launch 🚀 Time Profiler using your app
- Click record ⏺
- Play with you app as regular user 🤔(try various uses case)
- Stop ✋ the recording
- And then… you can filter 🕵️ by specific time intervals! 🤩
Here is an example of me recording as I create a new to-do in daily vibes and then filtering various time intervals. You can really isolate the various time intervals, like so:
As you can see, to filter 🔎 you simply move your mouse over to the timeline, click 👇 and drag 👉 in the direction of your interest. And, when you filter, if you notice 🧐, the tree changes accordingly. Thus you can really get into the nitty gritty details and find out your problems 🚧
Oh, anD… aNd… AND you can SAVE this sampling and come back to it later:
Here is an actual example of me diving deep into the tree 🌲. When you find something interesting, like this:
You can then double click 👇👇 on the method, and it will show you EXACTLY which lines 👀 of code that may be troubling:
There you have it friends. I’d like you to take away the following three things from this:
- Xcode is pretty awesome 🔥
- Be a digital detective 🕵️ and give Instruments a try
- Be careful ❌ of premature optimization
Liked this article? Click that 👏 below and it will let me know. I look forward to feedback so that I can improve in my future articles. You can also connect with me on twitter.
Until next time,
PS: I hope I did not offend you with the *for dummies tagline. I love technical documents but I feel like they need a bit of flavour sometimes. I simply want this technical mumbo-jumbo to feel relatable and friendly.
The initial version of this article was originally published on my blog. Thank you for giving me feedback! I incorporated some of your ideas in improving this article 💯