Pro ios Apps Performance Optimization Khang Vo Apress*
Contents Contents at a Glance About the Author About the Technical Reviewer Acknowledgments Preface iv ix x xi xii I Chapter 1: Introduction to ios Performance Optimization 1 A New Era of Smartphone 1 Why Performance Matters 1 Who Should Use This Book? 2 My Teaching Style 2 What Do You Need? 3 How to Use This Book 3 An Overview of the Book 3 Source Code 5 Contact the Author 5 Chapter 2: Benchmark Your Apps with Tools: Simulators and Real Device Test 7 Simulator and Device 8 Memory and Performance 8 Tools 9 Basic Tools 9 Memory Allocation 11 Legacy Code 13 Performance Tools 18 Summary 37 8 Chapter 3: Increase and Optimize UlTableView Performance 39 Introduction to the Examples 39 Reviewing the Instrument Tool 40 First Example 41 Second Example 50 What Can You Learn from These Examples? 54 v
CONTENTS Other Techniques 54 Caching the Height 54 Opaque 55 Avoid Graphical Effects 55 Performance for Editing/Reordering 56 Summary 57 i Chapter 4: Increase App Performance Using Image and Data Caching Techniques 59 Differences in Performance Between Network, File, and Memory Processing 60 Introduction to Caching 62 What is Caching? 62 Cache Hit 62 Cache Miss 62 Retrieval Cost 63 Storage Cost 63 Cache Invalidation 64 Caching Algorithms 65 Measuring Cache 71 What You Should Cache 72 Where Should You Store Your Images? 72 Data Caching 77 Summary 85 IChapter 5: Tune Your App Using Algorithms and Data Structures 87 First Example 88 Theoretical Issues of Measuring Algorithmic Performance 89 How to Measure Big-0 90 Implementation Details 92 Big-0 of Famous Algorithms 92 Practical Measurement 93 Data Structure and Algorithms 95 Cocoa Touch Data Structures 95 Other Data Structures 106 Binary Tree 119 Graph 123 Other Algorithms and Problem-Solving Approaches 130 Recursion 131 SAX/DOM Parser for XML Parsing 132 Summary 133 Chapter 6: Improve Parallel Data Access using Multithreading Techniques 137 What Are Threads and Multithreading? 138 Threading Terminology 139 First Example 140 Benefits of Multithreading 142 How to Write Multithreaded Applications 143 Create a Thread 143 Configuring a Thread 150 vi
CONTENTS Your Thread Entry 151 Risks of Threads 154 Thread Synchronization 169 Alternatives to Threads 171 Thread Instrument for iphone 174 Summary 174 Chapter 7: Optimize Memory Usage for Better Performance 177 A Little Review 178 Old Object Ownership Policy 178 Autorelease 178 Autorelease Pool 179 Automatic Reference Counting 180 ARC Policy 182 New Qualifier for ARC 182 Object Property 183 Advanced Memory Issues 184 Retain/Relationship Cycles 184 Weak References 185 UlViewController 185 Load View Process 185 Unload View Process 186 Displaying and Hiding Views in the User Interface 187 Object Copy 188 Shallow vs. Deep Copy 188 Implementing a Deep Copy 189 Integrating a Copy Method into an Object 190 Advanced Autorelease Pool 191 Instruments 192 Static Analyzer 193 Leak Instrument 193 Zombie 194 Object Allocation 194 Memory Warning Levels 195 Summary 196 (Chapter 8: Integrate Multithreading and Efficient Memory Usage for Multitasking Apps Performance 197 What is Multitasking in iphone? 198 Multitasking Handler Methods 202 Multitasking Benefits and Costs 204 Background Services 205 Audio Service 206 Show Splash Screen 207 Location Service 207 Local Notification 211 Voice Over IP (VOIP) 211 Background Execution 211 What to Notice when Running in Background 213 vii
i* CONTENTS System Changes Notification 215 Dealing with ios Versions 216 Summary 216 ' Chapter 9: Improve Performance with Native C/C++ 219 Benefits and Costs 220 Basic C and C++ programming 221 C Programming 221 C++ Programming 231 A Practical Example 236 SQLite 236 Integrate C++ into Your Application 238 Summary 238 Chapter 10: Comparing Android and Windows Phone Performance Problems 241 Benchmarking on Emulator and Devices 242 Emulator and Devices 242 Benchmarking 244 Android 246 Windows Phone 248 Data Caching 249 Android 250 Windows Phone 251 Data Structure and Algorithms 253 Multithreading 255 Android 255 Windows Phone 257 Memory Management 258 Android 259 Windows Phone 260 Multitasking 261 Android 261 Support of C/C++ Programming 262 Summary 263 Index 265 vlil