Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Swift High Performance

You're reading from   Swift High Performance Leverage Swift and enhance your code to take your applications to the next level

Arrow left icon
Product type Paperback
Published in Nov 2015
Publisher Packt
ISBN-13 9781785282201
Length 212 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
 Koval Koval
Author Profile Icon Koval
Koval
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Swift High Performance
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Exploring Swift's Power and Performance 2. Making a Good Application Architecture in Swift FREE CHAPTER 3. Testing and Identifying Slow Code with the Swift Toolkit 4. Improving Code Performance 5. Choosing the Correct Data Structure 6. Architecting Applications for High Performance 7. The Importance of Being Lazy 8. Discovering All the Underlying Swift Power Index

Avoiding Objective-C


You have learned that Objective-C (with its dynamic runtime) in most cases performs more slowly than Swift. Interoperability between Swift and Objective-C is done so seamlessly that sometimes we can use Objective-C types and its runtime in the Swift code without knowing that.

When you use Objective-C types in Swift code, Swift is actually using the Objective-C runtime for method dispatch. Because of that, Swift can't do the same optimization as for pure Swift types. Let's have a look at a simple example:

  for _ in 0...100 {
    _ = NSObject()
  }

Let's read this code and make some assumptions about how the Swift compiler would optimize that code. The NSObject instance is never used in the loop body, so we could eliminate creating an object. After that we would have an empty loop that could also be eliminated. So we would remove all the code from execution.

Let's see what is happening in reality by looking at generated assembly pseudocode:

rbx = 0x65;
  do {
    rax = [_OBJC_CLASS_...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Visually different images