Swifter Swift, Swiftly
Writing Swift code and worried about performance? Check out this collection of useful tips.
Join the DZone community and get the full member experience.Join For Free
Everybody wants their Swift programs to run swiftier, right? In case you haven’t stumbled across it so far, the canonical list of recommended ways to do that is found right in the Swift GitHub repo: Writing High-Performance Swift Code
So you should go read that at the first opportunity and keep an eye on edits too!
Follow that up with this excellently accessible talk: Real World Swift Performance
Lots of things can make your application slow. In this talk we’re going to explore application performance from the bottom. Looking at the real world performance impact of Swift features (Protocols, Generics, Structs, and Classes) in the context of data parsing, mapping, and persistence, we will identify the key bottlenecks as well as the performance gains that Swift gives us…
Here’s a couple more illuminating posts on instruction level speediness:
Mind you, it’s quite likely that for many of you the most compelling thing that springs to mind on reading the headline there is not swiftier execution, but swiftier compilation, amirite? Here’s some backgrounders and tips on that:
After I read @nickoneill’s excellent post Speeding Up Slow Swift Build Times last week, it’s hard not to look at Swift code in a slightly different light. A single line of what could be considered clean code now raises a new question — should it be refactored to 9 lines to please the compiler? (see the nil coalescing operator example further down) What is more important? Concise code or compiler friendly code? Well, it depends on project size and developer frustration.
Before getting to some examples, let me first mention that going through log files manually is very time consuming. Someone came up with a terminal command to make it easier but I took it a step further and threw together an Xcode plugin…
Also check out Swift build time optimizations — Part 2 for followup.
And you’re probably vaguely aware that XCTest does performance these days but haven’t adopted it wholeheartedly yet, right? Check out measure(): How to optimize our slow code and adjust the baseline.
XCTest makes performance testing extraordinarily easy: you give it a closure to run, and it will execute that code 10 times in a row. You’ll then get a report back of how long the call took on average, what the standard deviation was (how much variance there was between runs), and even how fast each of those 10 runs performed if you want the details…
It's handy to be able to get hard numbers for your refactoring efforts that easily, indeed.
Published at DZone with permission of Alex Curylo, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.