String, improving compilation time

Sayler8182
Sep 4, 2018 · 2 min read

Optimizing Build Times in Swift is a frequently discussed issue. I found a great article on this topic. @Tyler.milner paid attention to the long time of compilation for String interpolation and I decided to extend this part of story. In my previous article I’ve showed execution time of String interpolation. Now I will focus on build time.


But how to check how much time the compilation took? You can easily download and run a special tool BuildTimeAnalyzer (however, interference in the project settings will be required) or run script (that avoids this problem)

I’ve used the same model that I used earlier

This is the result

Adding interpolated strings is the least efficient. It is almost 70 times more than the fastest than String format. You can think it’s only 19ms, yes only 19ms, but still 70 times more than the fastest solution, and this is only one line of code. What if there were more than one? Let’s check it! I’ve copied each line 60 times.

The compiler has guessed how to handle third function, but now different between the fastest and the slowest is over 280ms. I have to admit that now it’s not 70 times, but only 24 times slower.


Conclusions

Don’t underestimate the compilation time. You can gain a lot at a low cost. In some cases, you can increase performance by up to several dozen percent by changing only a few lines of code.


Thank you for reading! If you liked this article, please clap so other people can read it too. I will also have more motivation for next article :) You can also see my other articles, maybe you will find something for you.

If you have any question or suggestion leave a comment.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade