Output compile durations for swift files
Asked Answered
C

1

17

Is there a way to output the time taken to compile a swift file during an xcode build?

I would like to compile from the command line to trigger the same build xcode does but to include the time taken to compile each file.

The Report Navigator show full build reports per file but there is no timeframe associated with them.

I would like to cut down the compile time of a Swift 1.2 project, as it takes around 5 - 10 minutes after a clean or 3 -5 minutes after changing source in a heavily dependent file.

Chantal answered 20/7, 2015 at 15:13 Comment(0)
G
45

You can add…

-Xfrontend -debug-time-function-bodies

…to Other Swift Flags in Swift Compiler - Custom Flags section (build settings).

Build settings

Note: You have to keep same order of these flags. -Xfrontend says that the next flag should be passed to the frontend. It will not work if you reverse the order.

Then you can get compile times in your build log:

Build log

Which is useful when you do want to optimize compile time and also it's good to attach this kind of build log when reporting an issue to Apple Swift guys about slow compile time.

Credit goes to Joe Pamer Errant compiler hacker. Currently an engineering manager at Apple (Swift, Clang), formerly at Microsoft (TypeScript, F#, JavaScript, .NET). He tweeted it as a response to Rob Rix question about profiling Swift compilation. It made me curious, so, I disassembled compiler, checked text section for more flags and found other hidden options. Don't use them in production code, just play with them.


Build time of the whole project. Run following command in terminal…

defaults write com.apple.dt.Xcode ShowBuildOperationDuration YES

…restart Xcode, clean & build and…

Build time


What if I have pods?

Do same as above for your pods project

Griffis answered 20/7, 2015 at 16:4 Comment(8)
Thanks. After scanning through compile logs of about 5 classes it gets difficult to manage. Is there a way to output the total time per file?Chantal
Updated my answer with description how to measure whole project build time (unsupported feature, can break in any future version). Checking if there's a way how to measure single file.Griffis
To close the case, there's no switch or plist setting to output compile time per file. Or I didn't find it. Maybe you can wrap swift[c] with some shell script to measure it (didn't try).Griffis
Travis CI does show the compile time per file! i guess they use the facebook xbuild toolJenaejenda
Other hidden options link seems to be brokenWhoops
Sry, I didn't migrate my old posts to Medium (yet). Here's the link to the repo with post: github.com/robertvojta/robertvojta.github.io/blob/master/_posts/…Griffis
Try to use this: xcodebuild -workspace App.xcworkspace -scheme App clean build OTHER_SWIFT_FLAGS="-Xfrontend -debug-time-function-bodies" | grep .[0-9]ms | grep -v ^0.[0-9]ms | sort -nr > culprits.txt (Source: irace.me/swift-profiling)Fordo
You said "You have to keep same order of these flags.", what flag order are you talking about? I only see one flag here -debug-time-function-bodies.Canadian

© 2022 - 2024 — McMap. All rights reserved.