Danger. Don’t Destroy Swift’s Performance with These mistakes
Perform well…
--
You can write functions and statements that don’t add any value to your code (at least as far as the Swift compiler is concerned). Since Swift can’t simply skip these functions you might see your performance dropping off.
Let us prevent this, let us put mitigations in place now. How? Speed is important so let’s read on and learn a few quick tips in Swift.
Difficulty: Beginner | Easy | Normal | Challenging
Prerequisites:
- Some understanding of Logging in Swift would be useful (guide HERE)
Terminology
Build: A build is a version of an App
Build Configuration: The configuration for an Xcode build
Debug: identify and remove errors
Logging: A way to capture log messages to memory and disk
Logging to the Console
Look. I’m as much a fan of logging
to the console to perform a quick debug
of your code as anyone.
In Swift this is often performed with a simple print
, rather like the following:
func test(input: String) {
print("The function \(#function) received \(input)")
}
Now the statement which follows is also true for NSLog
function (and although the Unified Logging System is designed not to sacrifice performance I’d recommend you don’t have logs in your production code in any case).
Valid instructions can’t be skipped, and therefore bring a performance penalty if used in production code.
Imagine if you have a print statement within a loop. Each time the loop is executed the print command will be executed. This is NOT good.