關(guān)于APP優(yōu)化,從wwdc內(nèi)容來看,主要是關(guān)注于APP啟動時間,CPU耗時操作,電量三個方面
對于啟動時間,wwdc2018里面提到app啟動動畫時間為600ms,其中100ms的時間用來加載我們的動態(tài)庫,以及一些靜態(tài)的對象的初始化,剩余的500ms我們可以進行第一屏展示的準備,所以我們的啟動操作最好控制在500ms以內(nèi)
啟動分為三種,冷啟動,熱啟動,暖啟動。冷啟動是第一次安裝App后的第一次啟動,熱啟動是程序從后臺再次進入前臺的啟動,這里一般不會有性能問題,暖啟動是我們最關(guān)心的,因為我們可以對其進行一些操作,來提高暖啟動的時間。
關(guān)于CPU耗時操作,wwdc提到,大多數(shù)代碼調(diào)用會有副作用,所以要關(guān)注你的代碼調(diào)用是不是造成了不必要的副作用
另外如果CPU占用過高,能延時的操作盡量延時操作,加載讀取數(shù)據(jù)盡量只讀取最少量的數(shù)據(jù)(數(shù)據(jù)夠用就可以了),比如photo中只加載當前要顯示的圖片,同時只加載低分辨率圖片,對于更新UI等操作,如果可以批量更新,就要批量更新,比如tableview的batchupdate方法。
使用適量的view,如果view太多,則應(yīng)當適當?shù)臏p少view,如wwdc中提到的photo引用中時刻從當前到年的轉(zhuǎn)換,將每月的低分辨率的圖片繪制到同一張圖上,同時記錄繪制的位置信息與圖片的索引,從而達到減少view的目的
另外我們要讓我們的CPU耗時可控,也就是優(yōu)化為常量時間
對于一次的計算結(jié)果,如果可以共享我們應(yīng)當避免多次計算,這在我們應(yīng)用中用的比較多的場景就是緩存cell的布局信息,避免多次計算
另外對于監(jiān)聽,使用NSNotification,KVO,delegate,block,我們應(yīng)該盡量避免使用KVO,使用KVO的好處是,信息更改時就通過runloop自動發(fā)出通知進行操作,但是很多情況下,我們并不需要這樣做,如果我們對實時性要求并不高,我們不需要根據(jù)每次更新都去改變,只需要保證最后一次的改變生效就可以,這時我們最好延遲的去通知最后一次的改變,因為每次更改都產(chǎn)生副作用,對我們的CPU進行了無謂的消耗
對于數(shù)據(jù)記錄,我們應(yīng)當優(yōu)先使用對象object,或者使用struct,而不是使用NSDictionary,因為對于很多的小對象,進行hash操作很耗時
我們優(yōu)化APP的操作是怎樣的。
首先我們應(yīng)該嘗試去復(fù)現(xiàn),找到復(fù)現(xiàn)的條件,模擬條件,保證可以復(fù)現(xiàn)操作
其次,我們通過profile去測試,記錄,分析
最后,我們?nèi)ソ鉀Q,解決問題,我們應(yīng)當采用直接簡單的方式。
第一,我們應(yīng)該考慮直接去掉有問題的代碼。如果去掉后,對實現(xiàn)沒有任何影響問題就解決了。
第二,如果不能直接去掉,我們細分操作,減少副作用,能不調(diào)用的就不調(diào)用,能延遲加載的就延遲加載,能批量操作就批量操作,能共享數(shù)據(jù)的就共享數(shù)據(jù)
第三,都不行的話,我們要從頭了解業(yè)務(wù)需求,嘗試重新設(shè)計
Instrument每1ms進行一次記錄,所以我們在Instrument中看到的最小耗時都是1ms,在Instrument里面每次記錄好像稱為Sample
Instrument中有很多過濾器,如Separate by Thread,該選項是默認勾選的,選擇該選項時,我們的方法列表會按Thread id進行分割,而不選中時,方法列表將按調(diào)用入口進行分割,這適合GCD的使用情況
我們在查找耗時操作時,最重要的就是聚焦,所以對于我們不關(guān)心的操作,我們應(yīng)該隱藏我們不關(guān)心的操作,比如一些系統(tǒng)庫的操作,或者第三方庫的操作,我們無法去修改的代碼,我們可以通過,右鍵然后charge...to caller的選項,charge...to...表示記在誰的賬上,這樣我們選中的操作,就被隱藏不再干擾我們的視野,同時她的耗時會記錄在他的調(diào)用者身上。
另外一種聚焦操作,就是屏蔽極細顆粒的操作,也就是屏蔽耗時極少的操作,這個可以通過Call Tree Constraints選項,前面說過Instrument的每次記錄就是一個Sample,所以如果我們設(shè)置了Sample的范圍,就可以將耗時集中在這個區(qū)間,比如我們選擇的范圍是(10~+∞),那么小于10ms的操作將不再展示出來。
Time Profile
wwdc中講的比較多的一個,可以在蘋果開發(fā)者,wwdc全部視頻搜索instrument去看一下,下面的文章是對wwdc2018和wwdc2019里面兩期內(nèi)容的總結(jié)
http://www.itdecent.cn/p/108c09e814de
1、操作的副作用可能造成耗時
2、不必要的反復(fù)執(zhí)行造成的耗時
3、本身是一個耗時操作
Allocations
https://developer.apple.com/library/archive/technotes/tn2434/_index.html
Consider ways to reduce this allocation.
1、Remove the allocation 不必要的初始化
2、Reduce the number of allocations 減少初始化的數(shù)量,比如可見的只有一個view,我們沒必要把所有的一起初始化
3、Reduce the size of the allocation,減少初始化的體積
Instrument help
https://help.apple.com/instruments/mac/current/#/dev7b09c84f5
Diggest
trick
使用Xcode Debug Navigator
使用view->zoom, view->track來放大縮小我們的track,使我們注意力集中
使用view->clear Inspection range關(guān)閉選中區(qū)
When viewing a call tree, many instruments include options to hide system calls and invert the call tree. These options are found in the Call Tree area under Display Options in the inspector pane. Hiding system calls allows you to quickly filter for calls made by your app. Inverting the call tree allows you to see the heaviest calls first.
Time Profiler
Track CPU core and thread use
Counters
Look for performance bottlenecks
Performance monitor counters (PMCs) are hardware registers that measure events occurring in the processor. They help find bottlenecks in your app by identifying an excessive number of events of a particular type. For example, a high number of conditional branch instructions may indicate a section of logic that, if rearranged, might lower the number of branches required. PMC events bring these issues to light, but it is up to you to match them to your code and decide how they will help you improve your app’s performance. The Counters profiling template uses the Counters instrument to track PMC events.
Activity Monitor:Use Activity Monitor to track overall network and disk use
The Activity Monitor profiling template uses the Activity Monitor instrument to track overall system activity over time, including CPU, memory, network, and disk. By default, the Activity Monitor template doesn’t display network or disk activity in the timeline pane. However, you can manually enable the display of these statistics.
Network :Monitor network connections of an iOS app
The Network profiling template uses the Connections instrument to analyze your iOS app’s TCP/IP and UDP/IP connections.
File Activity:Monitor disk use
The File Activity profiling template uses the File Activity, Reads/Writes, File Attributes, and Directory I/O instruments to watch your macOS app’s disk use.
Allocations
This instrument measures heap memory usage and tracks allocations, including specific object allocations by class.
Leaks
check for leaks—memory that has been allocated to objects that are no longer referenced and reachable.
Retain has been called on an object without a corresponding release call when the object is no longer referenced.
An object has been allocated and initialized with APIs that don’t cause the object to autorelease.
If a leak isn’t an object, you may be calling an API that assumes ownership of a malloc-created memory block, and you are missing a corresponding call to free().
Zombies
The Zombies profiling template uses the Allocations instrument to measure general memory usage in your app, with a focus on the detection of overreleased “zombie” objects—that is, objects that are called after they’ve been released and no longer exist.
An object has already been released (or autoreleased), and your app tries to release it again.
An object hasn’t been retained when it should have been.
Some other call is made to an object after it has been released.
Energy Log
The Energy Usage instrument indicates a level from 0 to 20, indicating how much energy your app is using at any given time. These numbers are subjective. If your app’s energy usage level is occasionally high, it doesn’t necessarily mean that your app has a problem. Your app may simply require more energy for some of the tasks it performs. For example, it may use the GPS while performing complex network operations. This is valid energy use. Look for spikes or areas of high energy use that are unexpected or that could be performed at more optimal times.
使用手機開發(fā)者模式監(jiān)測Energy,然后使用Instrument導(dǎo)入