[翻译] 5点建议,让iOS程序跑得更快
http://mobile.tutsplus.com/tutorials/iphone/ios-quick-tip-5-tips-to-increase-app-performance/
人翻胜于机翻,本着练习英语的目的翻译iOS相关的文章,看官切勿见笑。
本人将在文章的部分地方添加注释,并根据需求增减文章内容,在此对原作者辛勤劳作表示感谢
iOS Quick Tip: 5 Tips to Increase App Performance
Tutorial Details
- Difficulty: Intermediate
- Completion Time: 30 Minutes
- Technology: iOS SDK
Even though the iPhone 5 and the iPad 4 ship with a powerful A6(X) processor and a lot more RAM than the original iPhone, this doesn’t mean that iOS applications are by definition fast and performant. In this quick tip, I will give you five tips to improve application performance.
尽管iPhone5和iPad4装载了一个强力的A6(X)处理器,拥有着比以前iPhone更多的内存,你以为你的程序就能比以前跑得快了?代码写得烂一样跑不快哦.我会给你5点提示来让你的应用程序表现得更好.
1. Caching
If an iOS application fetches a remote resource, it is important that it doesn’t fetch that same resources every time it needs to access it. If you plan to develop a Twitter client for iOS, for example, then the client shouldn’t be downloading the same avatar every time it needs to display it. Caching the avatar will significantly improve application performance.
如果一款应用依靠着网络上的资源来运作,那么,你总不能每次都不停的获取(服务器上)相同的资源吧.如果你计划研发一款Twitter的客户端软件,那么,这个客户端就不应该每次都下载相同的数据来显示它.缓存这段数据就是个非常有效的方法.
What is caching? The basic idea of caching is simple. When the application fetches a remote resource for the first time, it stores a copy of that resource on disk for later use. If that same resource is needed later, the application can load it from disk instead of fetching it remotely. The added benefit is that the resource is also available when the device is not connected to the web. The complex aspect of caching is how to store the resource, when to store it, and, most importantly, when to refresh it.
什么是缓存?原理是很简单的.但应用软件第一次获取服务器上的资源时,它就存储了一份拷贝在闪存上方便以后的使用.以后的某段事件你又需要用到那段数据,应用软件就直接从闪存中读取出来而不用再次从服务器中获取.另外一点的好处呢,就是即使你的应用连接不上网络获取不了资源数据,你本地上还是有得.恩,难点呢,就是怎么去缓存数据,什么时候去缓存它,最重要的是,什么时候刷新它.
Olivier Poitrey has developed a small library named SDWebImage that is aimed at caching images. It provides a category on UIImageView
much like AFNetworking does. The key difference is that SDWebImage caches the images it downloads. Images are downloaded in the background and each images is decompressed, resulting in faster load times. The library leverages Grand Central Dispatch to keep the main thread responsive. If your application works with remote images, then you should take a look at this gem.
Olivier Poitrey 已经搞出了一个很小的库,名叫SDWebImage,专门用来缓存图片的.他给UIImageView提供了一个类目就像AFNetworking所做的那样.但他们有着一个核心的不同点:SDWebImage能缓存已经下载过的图片.图片在后台下载完之后再进行解压,目的是为了应用程序快速的加载.这个库会促使GCD(多核编程)保持与主线程良好的响应,如果你的应用程序需要经常加载服务器的图片资源,也许你应该瞧瞧这个不错的库.
2. Do Not Block the Main Thread
One of the most important lessons to learn when developing for the iOS platform is to never block the main thread. If you block the main thread with a long running operation, such as downloading images or other resources, your application will become unresponsive as long as the operation isn’t completed. Make it a habit to move long running tasks to a background thread. Even if you only need a handful of bytes, if the device’s connection to the web is slow, the operation will still block the main thread.
请记住一点,永远不要阻塞主线程.如果你执行了一段运行时很长的代码而阻塞了主线程,例如下载图片资源或者其他资源,你的应用程序将会变得反应迟钝直到你的那段代码执行完毕了.把那段代码转移到后台的线程去吧.即使是你仅仅想获取一点点字节数的资源,如果连接那个网站很慢呢?这个代码一样也会阻塞主线程的.
Writing safe multithreaded code has become a lot easier with the introduction of Grand Central Dispatch. Take a look at the following code snippets. The first snippet downloads data on the main thread, while the second snippet leverages Grand Central Dispatch to perform the same task on a background queue.
如今,GCD已经介绍了,写出安全的多线程代码已经变得很容易,看看下面的代码片段吧.第一段代码是在主线程中下载数据,同时,第二段代码促使GCD在后台队列中做着同样的工作.
NSData *data = [NSData dataWithContentsOfURL:URL];
3. Lazy Loading
Being lazy isn’t always bad especially if you are a programmer. Lazy loading is a well known concept in software development. It simply means that you postpone the instantiation of an object until you need the object. This is advantageous for several reasons. Depending on the object and the cost for instantiation, it can dramatically improve application performance and memory usage. Lazy loading objects isn’t a difficult concept. The simplest implementation of this pattern is overriding the getter of a property as shown below.
如果你是一个码农,偶尔懒惰一下也不是什么坏事.在应用软件领域,延迟加载可谓家喻户晓.很简单,你延时实例化一个对象,直到你真的需要了.他有着几点特别的好处.因为实例化一个对象是需要开销的,(延迟实例化它)能够戏剧性的提升你应用程序的流畅度以及良好的内存使用率.延迟加载对象不是什么难事.最简单的实现就是去重写一个属性的getter方法,看看下面的代码吧.
- (MyClass *)myObject
The lazy loading pattern can be applied in many areas of software development. For example, if part of your application’s user interface is not shown by default, it may be advantageous to postpone its instantiation until that part is about to be shown.
延迟加载的设计方式可以用于很多的软件开发领域.例如,如果你的应用程序的部分UI界面还没有显示出来,那么,当真正到了需要显示的时候,那就实例化出那个对象来显示吧.
Table and collection views use a combination of caching and lazy loading to optimize performance. If the next cell or item is about to be displayed on screen, the table or collection view looks for a cell or item it can reuse (caching). Only if no reusable cell or item is available will the table or collection view (or its data source) instantiate a new cell or item (lazy loading).
table和视图集会用到缓存以及延迟加载来优化程序的性能.如果下一个单元或者物件将要在屏幕上显示出来了,那么table或者视图集就可以重用.仅仅在没有可以重用的单元或者物件的时候,这个时候就创建出一个新的单元或者物件(之后可以重用了).
4. Measure Performance
If you notice that your application is slow at times and you want to find out what is causing it, then you may need to profile your application with a tool such as Instruments. Colin Ruffenach wrote a nice tutorial about time profiling with Instruments on Mobiletuts+.
有时候,你注意到了你的应用程序偶尔会卡顿而且你也想找出到底是什么东西引起的.那么,你就需要一个工具来分析了,例如Instruments.Colin Ruffenach 在网站Mobiletuts+写了 <a nice tutorial about time profiling> 来教你使用 Instruments .
If Instruments doesn’t give you a clear answer, you may be interested in MGBenchmark, a benchmarking library developed and maintained by Mattes Groeger. This compact library lets you measure how fast your code executes and it helps you to track down the bottlenecks in your code base. It actually complements Instruments quite well so you shouldn’t use one or the other. Mattes Groeger’s library comes with an extensive feature set and is remarkably powerful. I highly recommend it as an alternative or complement to Instruments.
如果 Instruments 还是不能给你一个清晰的答案,也许你会对MGBenchmark感兴趣,那就看看Mattes Groeger大神所开发和维护的企业级库,这个牛逼的库能够测试出你的代码能够跑得多快,而且,他还能追踪到你代码的最高运行速度.他真的表现得完美,你根本就不需要其他的了,真的.Mattes Groeger的库有着大量的牛逼的特性,功能非常强大.在这里,我无节操的强烈向你们推荐它.
5. Stress Testing
If you’re developing an application that works with lots of data, then it is important to test it with…well…lots of data! During the development of an application, it isn’t always easy or possible to imagine how your users are going to use your application, but it is good to put it through its paces before releasing it. Your application may perform admirably during development and perform terribly in situations where it is flooded with data. You probably won’t be able to predict, let alone test, all the circumstances in which your application will be used, but you can try to mimic common situations by creating data sets to test with.
如果你开发的应用软件需要处理非常多的数据,好吧,那你就用巨量的数据来测试它!在软件开发阶段,想象着用户怎么暴力玩弄你的软件似乎有点困难,但是呢,发布软件前最好还是进行压力测试.也许开发阶段,应用软件表现良好,但是呢,一旦遇到了海量的数据量时,估计会卡得掉渣.你很有可能无法预测到这点哦,那就进行测试吧,将所有可能出现的情形都测测,有时候,你可以(自己)创建出一些测试数据来模拟现实的情形进行压力测试.
I often create a small script or class that populates the application with test data to measure how well it performs with a lot of data. Ideally, you want to populate the application with more test data than an average user will have – to the extent that you can predict this during development. When I was developing Pixelsync, an iPad application that interfaces with Aperture and iPhoto, I created an Aperture library with close to 500,000 images. If Pixelsync performed well with a photo library of that size, I could be fairly confident that the average user wouldn’t run into performance issues.
我常常会写脚本来装载进程序中来测设数据,测量测量应用程序在面对巨量数据时是否能表现如意.理论上,你需要给出比正常需求更多的数据进行测试.再者,你可以在开发阶段就提前预测.当我在开发Pixelsync(一款iPad应用,可以与Aperture和iPhoto交互)时,我创建了一个Aperture的库,足足有50000张图片.如果Pixelsync能够处理好如此巨量的图片库,那我能十分确信,即使是春哥也玩不坏这款软件.
If you really are a lazy developer or you just don’t have enough time, then you can also be more creative by building a robot to do the job for you.
如果你真的懒得要死,或者要打游戏没有时间,也许,你可以自己造一个机器人来帮你干这些事情......
Conclusion
Application performance remains an essential aspect of software development despite the fact that the new generation of devices are much more powerful and capable. Blocking the main thread, for example, will always result in a bad user experience no matter how capable the device is.
开发应用软件时,软件运行得流畅是非常非常非常重要的,尽管,事实上,每更新一代硬件后,性能都会得到提升.但是,垃圾代码阻塞了主线程,那卡顿的触感是会让用户崩溃而问候开发人员的,无论你硬件的性能有多牛逼.
[翻译] 5点建议,让iOS程序跑得更快的更多相关文章
- 让DB2跑得更快——DB2内部解析与性能优化
让DB2跑得更快——DB2内部解析与性能优化 (DB2数据库领域的精彩强音,DB2技巧精髓的热心分享,资深数据库专家牛新庄.干毅民.成孜论.唐志刚联袂推荐!) 洪烨著 2013年10月出版 定价:7 ...
- 面试官:如何写出让 CPU 跑得更快的代码?
前言 代码都是由 CPU 跑起来的,我们代码写的好与坏就决定了 CPU 的执行效率,特别是在编写计算密集型的程序,更要注重 CPU 的执行效率,否则将会大大影响系统性能. CPU 内部嵌入了 CPU ...
- UOJ 【UR #5】怎样跑得更快
[UOJ#62]怎样跑得更快 题面 这个题让人有高斯消元的冲动,但肯定是不行的. 这个题算是莫比乌斯反演的一个非常巧妙的应用(不看题解不会做). 套路1: 因为\(b(i)\)能表达成一系列\(x(i ...
- 【UOJ#62】【UR #5】怎样跑得更快(莫比乌斯反演)
[UOJ#62][UR #5]怎样跑得更快(莫比乌斯反演) 题面 UOJ 题解 众所周知,\(lcm(i,j)=\frac{ij}{gcd(i,j)}\),于是原式就变成了: \[\sum_{j=1} ...
- 「UR#5」怎样跑得更快
「UR#5」怎样跑得更快 膜这个您就会了 下面是复读机mangoyang 我们要求 \[ \sum_{j=1}^n \gcd(i,j)^{c-d} j^d x_j=\frac{b_i}{i^d} \] ...
- 安装好Windows 8后必做的几件事情,让你的Win8跑的更快更流畅。
1.关闭家庭组,因为这功能会导致硬盘和CPU处于高负荷状态. 关闭方法:Win+C-设置-更改电脑设置-家庭组-离开 如果用不到家庭组可以直接把家庭组服务也给关闭了:控制面板-管理工具-服务-Home ...
- 让你的 Node.js 应用跑得更快的 10 个技巧(转)
Node.js 受益于它的事件驱动和异步的特征,已经很快了.但是,在现代网络中只是快是不行的.如果你打算用 Node.js 开发你的下一个Web 应用的话,那么你就应该无所不用其极,让你的应用更快,异 ...
- 让MySQL数据库跑的更快的7个优化建议!
随着容量和负载的增加,MySQL 的性能会日趋缓慢.这里有七点建议能够保证 MySQL 的平稳运行. 性能是我们衡量应用的一种方式,而应用性能的一项指标就是用户体验,也就是平时我们常说的:“用户需要等 ...
- 如何让python程序运行得更快
原则1:不优化 原则2:不要优化那些不重要的部分(否则会降低可读性) 解决方案: 1. 使用函数,局部变量比全局变量快很多.尽量使用函数,如main() 2. 有选择性的消除属性访问. 如多用 fro ...
随机推荐
- 【AtCoder】ARC062F - AtCoDeerくんとグラフ色塗り / Painting Graphs with AtCoDeer
题解 考虑一个点双(因为是简单环),如果没有环(两点一线),那么乘上K 如果有一个环,那么用polya定理,每个置换圈有gcd(i,n)个循环节 如果有两个及以上的环,任何一种置换都合法,那么只和每个 ...
- USACO 5.4 Canada Tour
Canada Tour You have won a contest sponsored by an airline. The prize is a ticket to travel around C ...
- Java人员正确使用 IntelliJ IDEA的方式
原文: http://tengj.top/2017/02/22/idea1-1/ 作者: 嘟嘟MD 前言 博主是Java开发人员,以前一直都用myeclipse来开发的,说实话感觉myeclipse毫 ...
- 问题:SpringBoot访问不到Controller
SpringBoot正常启动,其它配置都正常,以下是控制台打印: 解决方法: 将controller与application配置文件同层,是访问时无法扫描到相应的controller,故无法映射到相应 ...
- source Insight 软件使用注意点
1. 需要将 tab键转为 4个空格 首先通过路径(Options->Document Options)进入以下界面: step 1:将 Visible tabs 打勾. step 2 :将 E ...
- FPGA In/Out Delay Timing Constaint
先简单说说这段时间遇到的问题.FPGA采集前端scaler的视频数据.像素时钟(随路时钟),视频数据,行场同步,DE.这些信号进入FPGA后.通过CSC(颜色空间转换).输出后的图像有噪点.通过查看时 ...
- ARM Linux 驱动Input子系统之按键驱动测试
上一篇已经谈过,在现内核的中引入设备树之后对于内核驱动的编写,主要集中在硬件接口的配置上了即xxxx.dts文件的编写. 在自己的开发板上移植按键驱动: 1.根据开发板的原理图 确定按键的硬件接口为: ...
- KVM libvirt的CPU热添加
一. NUMA1. NUMA 介绍 早期的时候,每台服务器都是单CPU,随着技术的发展,出现了多CPU共同工作的需求. NUMA(Non-Uniform Memory Access,非一致 ...
- [BZOJ2815][ZJOI2012]灾难(拓扑排序/支配树)
支配树目前只见到这一个应用,那就不独分一类,直接作为拓扑排序题好了. 每个点向所有食物连边,定义fa[x]为x的支配点,即离x最近的点,满足若fa[x]灭绝,则x也要灭绝. 这样,将fa[x]向x连边 ...
- POJ 3155 Hard Life 最大密度子图 最大权闭合图 网络流 二分
http://poj.org/problem?id=3155 最大密度子图和最大权闭合图性质很相近(大概可以这么说吧),一个是取最多的边一个是取最多有正贡献的点,而且都是有选一种必须选另一种的限制,一 ...