Book Image

Swift High Performance

By : Kostiantyn Koval
Book Image

Swift High Performance

By: Kostiantyn Koval

Overview of this book

Swift is one of the most popular and powerful programming languages for building iOS and Mac OS applications, and continues to evolve with new features and capabilities. Swift is considered a replacement to Objective-C and has performance advantages over Objective-C and Python. Swift adopts safe programming patterns and adds modern features to make programming easier, more flexible, and more fun. Develop Swift and discover best practices that allow you to build solid applications and optimize their performance. First, a few of performance characteristics of Swift will be explained. You will implement new tools available in Swift, including Playgrounds and REPL. These will improve your code efficiency, enable you to analyse Swift code, and enhance performance. Next, the importance of building solid applications using multithreading concurrency and multi-core device architecture is covered, before moving on to best practices and techniques that you should utilize when building high performance applications, such as concurrency and lazy-loading. Finally, you will explore the underlying structure of Swift further, and learn how to disassemble and compile Swift code.
Table of Contents (15 chapters)
Swift High Performance
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Accelerate and Surge


Both iOS and OS X SDK have a very powerful framework that provides high-performance functions for working with matrices, vectors, signals, image processing, and math operations. It is called the Accelerate framework. The Accelerate framework is quite big, so we will take a look at only one part that is related to working with collections; it is the vDSP part. You can find out more about it at https://developer.apple.com/library/prerelease/ios/documentation/Accelerate/Reference/vDSPRef/index.html.

First, let's implement the very basic mapping, calculating, and sum operations using the Swift standard library:

let array = [1.0, 2.0]
let result = array.map { $0 + 3.0 }
result // [4.0, 5.0]

let sum = array.reduce(0, combine: +)
sum // 3

This code is very clear and readable and doesn't need any comments. Let's try to do the same using Accelerate:

let array = [1.0, 2.0]
var result = [Double](count: array.count, repeatedValue: 0.0)

var add = 3.0
vDSP_vsaddD(array, 1, &add...