Source: The old boy under the tree
Links: HTTP://WWW.JIANSHU.COM/P/5CF9AC335AEC
The iphone app has always been known for its smooth operating experience, but since developers have focused more on development features than on performance, this may involve objective-c,c++ and Lua, which are relatively complex to optimize. resulting in applications such as touch and other lower end of the product, light from the start to the page to take nearly a minute, the switch between the page is not that very smooth feeling, memory is also high, the impact of the user experience of the application, so it is necessary to do some optimization, Let's take a look at some of the things I've learned during the optimization process:
1 instruments
In the performance analysis on iOS, first consider the use of instruments this tool to analyze the problem where, do not imagine, otherwise you may spend 1% of the problem, and finally found that nothing is optimized, such as to see which parts of the program is the most time-consuming, you can use Profiler, to see if the memory is leaking, you can use leaks and so on. There is a lot of information on instruments online, as a qualified iOS developer, it is necessary to be familiar with this tool.
2 do not block the main thread
The Uikit operation on iOS is always on the main thread, so if the main thread is blocked, your UI may not respond to events in a timely manner, giving a feeling of lag. Most of the blocking main thread is the main thread to do IO operations, such as file read and write, including database, image, JSON text or log logs, and so on, as far as possible to put these operations on child threads (if the database has more operations, remember to use transactions to deal with, performance difference is quite large), or set up a corresponding dispatch queue in the background to do this, such as a low-level serial queue to be responsible for log file records and so on. In the program if your code logic is written according to the logic of synchronization, try to modify the logic code it ...
3 using the Cache
In general, in order to improve the user experience, will be used in the application of the cache, for example, the image resources can use sdwebimage this open source Library, the implementation of a picture cache function. Refer to the Sdwebimage code itself can also implement the cache function:
Cache simple
The business layer obtains the corresponding resources according to the resource URL to the ResourceManager, ResourceManager first will go to memorycache this side to obtain the resources, MemoryCache may use nscache realization, Because Nscache is thread-safe first, it releases the corresponding memory when the memory warning is received, and if MemoryCache does not have the corresponding resources to disk, then the disk does not have the words to go to the Internet to obtain, Get the words will be updated to MemoryCache and disk, specifically to refer to the Sdwebimage implementation details.
4 Reducing the task during program startup
When the user clicks on the app's icon, the program should go to the main page as soon as possible, to minimize the user's waiting time, such as our application in the launch of the 3d model to do the rendering operation, after the completion of the first page to show, but in fact, we can first go to the homepage, The task of rendering 3d is placed on a child thread to complete, shortening the time that the user needs to wait.
3d
5 using the appropriate data structure
Depending on the business scenario to choose the right data structure, you may not see the difference in the amount of data, but if you store a large amount of data and complex data structure, this may affect your program performance. The more commonly used data structure is the array, but we know that its lookup complexity is O (n), so if you need to quickly find an element, you can use map. You can refer to the Apple collections programming Topics.
6 Memory
The arc used in general development does not require developers to focus on the creation and release of memory, but if you are using MRC and are mixed with other languages (such as C + + and LUA), how to ensure that memory is properly released is something you need to consider. Sometimes some memory leaks instruments may not be accurate analysis, then you need to troubleshoot, you can use the method swizzling to help us troubleshoot memory leaks, to ensure the correct operation of the program.
7 Lazy Load View
Do not nest too many view in the cell, this will affect the smoothness of the slide, and more view also need to spend more CPU and memory. If the slide is not smooth due to too much view, do not create all of the view at once, and put some of the view into the cell when it needs to be displayed.
8 LUA optimization
Because the business of the project is and some of the framework is implemented in Lua, so also by the way, the problem with Lua. Lua claims to be the fastest scripting language, there is no general performance problem, if the LUA code to optimize, there are a lot of this optimization point of attention, this time I mainly say a possible impact on the performance of the point-lua garbage collection. Garbage collection is a time-consuming operation, if the garbage collection operation is too frequent will inevitably affect the operation of the program, such as the ipod in the use of Lua_cjson parsing a 4.7M JSON file is spent 3.43s time, and later found to be related to garbage collection. General memory usage is moderate, can not go to him, let Lua incremental mode to deal with it, normal situation this will work better; If you want to control the GC's operation, you can set the GC parameters, these parameters may have a certain relationship with the project, You can experiment with the best value.
GC parameter settings, according to the situation to take the best value
CollectGarbage("Setpause", "Max" )
CollectGarbage("Setstepmul", $ )
The above is a summary of some of my records in the optimization process, about the iOS graphics performance this block of optimization can be viewed here (http://www.jianshu.com/p/2a01e5e2141f), if there is any performance optimization ideas can also be proposed ~?? (ps:you)
The things that app performance optimizes