[ACCEPTED]-How to decrease build times / speed up compile time in Xcode?-performance

Accepted answer
Score: 56

Often, the largest thing you can do is to 8 control your inclusion of header files.

Including 7 "extra" header files in source 6 code dramatically slows down the compilation. This 5 also tends to increase the time required 4 for dependency checking.

Also, using forward declaration instead 3 of having headers include other headers 2 can dramatically reduce the number of dependencies, and 1 help all of your timings.

Score: 24

I wrote an extensive blog post about how 4 I improved the iOS development cycle at 3 Spotify:

Shaving off 50% waiting time from the iOS Edit-Build-Test cycle

It boiled down to:

1) Stop generating 2 dSYM bundles.

2) Avoid compiling with -O4 1 if using Clang.

Score: 17

Personally I switched compiler to LLVM-Clang 11 for my Mac development projects and have 10 seen a dramatic decrease in build times. There's 9 also the LLVM-GCC compiler but I'm not sure 8 this would help with build times, still 7 that's something you can try too if LLVM-Clang 6 does not work for iPhone app compilation.

I'm 5 not 100% sure LLVM is supported for development 4 on the iPhone but I think I remember reading 3 in a news feed that it is. That's not an 2 optimization you can implement in your code 1 but it's worth the try!

Score: 12

The number of threads Xcode will use to 13 perform tasks defaults to the same number 12 of cores your CPU has. For example, a Mac 11 with an Intel Core i7 has two cores, so 10 by default Xcode will use a maximum of two 9 threads. Since compile times are often I/O-bound 8 rather than CPU-bound, increasing the number 7 of threads Xcode uses can provide a significant 6 performance boost for compiles.

Try configuring 5 Xcode to use 3, 4 or 8 threads and see which 4 one provides the best performance for your 3 use case.

You can set the number of processes 2 Xcode uses from Terminal as follows:

defaults write com.apple.Xcode PBXNumberOfParallelBuildSubtasks 4

Please 1 see Xcode User Defaults for more information.

Score: 12

If you're not using 8GB of RAM, upgrade 6 now.

I just upgraded my macbook pro from 5 4GB to 8GB. My project build time went 4 from 2:10 to 0:45. I was floored by the 3 improvement. It also makes web browsing 2 for research snappier and general Xcode 1 performance when indexing, etc.

Score: 11

Easy answer: add another machine running 19 Xcode on your local network. Xcode incorporates 18 distcc to do distributed compiles. It can 17 even use Bonjour to find other build hosts, which 16 simplifies the process of configuring this 15 greatly. For large builds, distributing 14 can get you a speed increase that is nearly 13 linearly proportional to the number of build 12 machines (2 machines takes half the time, three 11 takes a third and so on).

To see how to set 10 this up, you can consult this development doc. It also features 9 other useful build time improvement strategies, such 8 as using precompiled headers and predictive 7 builds.

Edit: Sadly, it appears Apple has 6 removed this feature as of Xcode 4.3: http://lists.apple.com/archives/xcode-users/2012/Mar/msg00048.html

Xcode 5 5 has a server version which can do CI, but 4 I doubt this will confer any benefit for 3 ad hoc developer builds. However, there 2 are some unannounced features that should 1 dramatically speed up build times.

Score: 7

One huge tip to halve compile times (for 6 iOS projects at least) is to set Build Settings / Architectures / Build Active Architecture Only to YES.

What 5 this does (especially with the advent of 4 64-bit iPads/64-bit compiler) is to not build the 3 binary for the architectures you're not 2 currently using.

Make sure you remember to re-enable this setting on submission to the 1 app store, or your binary will not validate.

Score: 7

I used a script to make use of a RAM drive, together 17 with some "forward declarations" optimizations 16 my project clean build time went from 53 seconds 15 to 20 seconds.

I was tempted to get the Gui 14 on the AppStore, but opted rather to go 13 for command line. I put the script as part 12 of git repository.

To see the build times, enter 11 this in a terminal: "defaults write com.apple.dt.Xcode ShowBuildOperationDuration YES"

Restart Xcode to notice 10 the build times in the toolbar. (this is 9 my non clean build time using objective-c) Cached build times

Adjust 8 the script to your liking. - Note the script clears the derived data folder.

#!/bin/sh

#2 GIG RAM
GIGA_BYTES=$((2*1024*1024*1024))

# a sector is 512 bytes
NUMSECTORS=$((${GIGA_BYTES}/512))

#ram disk
mydev=`hdiutil attach -nomount ram://$NUMSECTORS`
newfs_hfs $mydev

# make mount point
MOUNT_POINT=/Users/your_user_name/Library/Developer/Xcode/DerivedData

# ******************************************* 
# ** WARNING - MOUNT POINT WILL BE DELETED ** 
# *******************************************
rm -rf ${MOUNT_POINT}
mkdir -p ${MOUNT_POINT}

# mount
mount -t hfs $mydev ${MOUNT_POINT}
echo unmount $(MOUNT_POINT)

To see the effect and to control the RAM Drive:

mount                       - see mount points
umount mount_point          - unmount point
diskutil list               - see disks
diskutil eject /dev/diskX   - eject the disk
df -ahl                     - see free space

NOTE: I essentially 7 use the hdiutil provided by macOs. I tried 6 switching the -kernel option (no swapping 5 to disk) on but failed on my machine, saying 4 it is not implemented.

Maybe the new OS 3 coming soon we will see even more improvements 2 as the new file system copy feature is really 1 fast, and possibly makes this script redundant.

Score: 2

You mentioned using static libs for your 8 most-often used files to prevent compilation. You 7 can accomplish something similar by putting 6 headers to your code that it's frequently 5 used but not in your static libs in the 4 precompiled header. At least they'll only 3 be compiled once.

Care must be taken to avoid 2 issues if you have multiple compilation 1 types across your project (e.g. Obj-C, Obj-C++, C++).

Score: 2

Hey there, I would recommend you to optimize 7 your project's physical structure. There's 6 some good reading about this ( at least 5 in the C++ world ) , but I do objective-C 4 and the same principles often apply.

Here's 3 a great article about project's physical 2 structure optimization, which tends to improve 1 compile times Games From Within: Physical Structure Part 1

Good luck :)

Score: 2

one word: TmpDisk

  1. Use TmpDisk to Create a 1.5Gb RAM disk
  2. Change Xcode > Preferences > Location > Derived Data to /Volumes/1.5Gb/xcode data
  3. Enjoy the speed!

0

Score: 1

Quick Note Regarding 'Throw more hardware 25 at it' approach..

SUMMARY: I experienced 24 a SMALL speed increase from making a SIGNIFICANT 23 hardware upgrade

Test: Build/Run the exact 22 same project on cloned macbooks (where the 21 only difference should be their hardware)

Old 20 Macbook Air (1.86GHZ Core 2 Duo ONLY 2GB 19 RAM) vs Brand New Macbook Pro (2.3GHZ Core 18 i7 8GB RAM)

BUILDING ON IPHONE 3GS
Macbook 17 Air 1:00 - 1:15
Macbook Pro ~1:00

=> 0 to 16 0:15 of speed increase

BUILDING ON IPHONE 15 4S
Macbook Pro ~0:35
Macbook Air ~0:50

=> ~15 14 seconds of speed increase

**Partially tested: There 13 DOES apear to a significant difference between 12 build times for the SIMULATOR between the 11 2 machines


In my continued experience.. you 10 WILL get a significant increase when making 9 big changes in PHONE hardware (i.e. build 8 time on a 3GS vs iphone 5 (or 4 for that 7 matter)).. at least in my experience, the 6 limiting factor was the phone hardware (not 5 the computer hardware).

SO.. to get the fastest 4 build time..
option1) write code and run 3 in the simulater on a fast computer OR
option 2 2) build on the device with the lastest 1 iphone

Score: 1

If your whole project gets rebuilt every 6 time you hit run, that's probably the bug 5 in XCode 7.0 <= 8.1 giving you a hard 4 time.

Creating the user defined build setting 3 HEADERMAP_USES_VFS to YES cut the macbook 2 compile time from 75 seconds each time, to 1 25 seconds. See Xcode 8 does full project rebuild for more info.

More Related questions