It is time for change

1) Measure your compile time correctly ⏱️

To improve your compile time you have to know how long your project takes to compile. To measure your compile time open your terminal and enter



defaults write com.apple.dt.Xcode ShowBuildOperationDuration YES

After you have enabled your timer you should see your compile time in Xcode.

Compile time in Xcode (Without cleaning)

The compile time is pretty low. Too low. If you want a more accurate compile time from Xcode you should clean your build folder before measuring the time. Just use ⌘+⌥+K

Compile time in Xcode. After cleaning (including the build folder)

But if you want the most accurate compile time from Xcode you should clean your Xcode Project (including the build folder - ⌘+⌥+K ) and delete your derived data. Just open your terminal and enter

rm -rf ~/Library/Developer/Xcode/DerivedData

Compile time in Xcode. After cleaning (including the build folder) and deleting the derived data

When measuring your compile time you should always clean your project (including the build folder) and delete your derived data. This is the method I am using while writing this article.

2) Get a warning for slow code ⚠️

Xcode is actually pretty smart and I like this feature really much. Xcode is able to identify code that is compiling really slow. You can activate this by adding the following lines into your Build setting under Other Swift Flags

-Xfrontend -warn-long-function-bodies=50

-Xfrontend -warn-long-expression-type-checking=50

If a function or expression needs more than 50 milliseconds to compile you’ll get a warning. (Normally you would take a higher number but I like to get more warnings to improve my code faster…)

A random function that takes too long to compile

3) The basic improvements 😃

Enable Xcode “new” Feature to concurrent build tasks.

Just enter the following command into your terminal

defaults write com.apple.dt.Xcode BuildSystemScheduleInherentlyParallelCommandsExclusively -bool NO

This “new” Feature was released with Xcode 9.2 (Release Notes) and they mentioned that it could also slow down some of your projects because it is using more memory. To disable it just enter the following command into your terminal

defaults delete com.apple.dt.Xcode BuildSystemScheduleInherentlyParallelCommandsExclusively

(FYI: I used Xcode 10.3 (10G8) when writing this article and I thought this feature is enabled by default now… But it wasn’t).

4) Some coding guidelines 📜

Now let’s dive deeper into Swift.

Use let, private etc. whenever you can.

Xcode normally tells you that you should use let instead of var but sometimes it doesn’t…

final class ViewController: UIViewController {

var lol = Lol()

var lol2 = Lol2()

.....

var lol100 = Lol100() }

After changing the vars to let my compile time went from about 6.5s to 6.3s.

Make your classes final (If wanted).

Final Classes are increasing your performance and decreasing the compile time. Apple Docs

We started to make all our classes final (and remove the final keyword if we want to overwrite a class)

The thing with type inference.

I will make it short. Stop using variables without declaring it’s type. If you don’t do it, the compiler has to… And surprise. It will increase your compile time.

No, no, no. Stop 🛑

var name = getName()

Are you a senior developer?!

var name: String = getName()

It’s time to thank some people who “helped” me with this article and optimizing my compile times…

Thanks to Josh Gare for his great medium article:

Thanks to Tyler Milner for his great medium article:

Thanks Patrick Balestra for his awesome blog post: