Some views on IOS performance optimization


In our normal development work, the development time is very tight when the requirements are set, so we will seize the time to develop and complete the requirements. In the process of rapid development, there will be some performance problems more or less. After the development task is completed, we will carry outperformance optimization。 Now I share the performance optimization problems in the development process as follows.

The overall picture of performance optimization is as follows:

Some views on IOS performance optimization

1、 Data compression

In the process of running a program, the transmission of data is also an aspect that affects the performance of the program.In the case of constant transmission speed, the amount of data is large, the transmission time is more, the amount of data is small, and the transmission time is naturally less。 It takes less time to pass in, so the response speed of our program will naturally become faster.

1. Compress the data transmitted by the network

This step needs to cooperate with the server, which needs to enable the configuration of data compression. For the mobile end, online open sourceAFNetWorkingThe framework has helped us realize this function. We just need to enable this function in the network request base class we defined.

The code is as follows:

         *Request data to enable gzip compression
        [self.sessionManager.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];

2. Compress the downloaded pictures

This step improves the performance of the program obviously. My previous company’s app was auctioned. The photos were taken in a very fine way. Some of the photos were several meters long. They were downloaded to the app. As soon as the pictures were parsed, they flashed back. We have been tracking, this kind of big picture download down a resolution, cell phone memory directly burst. So we compressed the downloaded pictures.

2.1 compress the size of the picture

We discussed with the server and improved it. When we request to download the image, add the size of the image, and the server will return the corresponding size of the image to us.

Optimization scheme of client:According to the width of the screen and the size given by the designer, calculate the image size required by the current mobile phone, and then bring the size to request the server to download the image

2.2 compress the data amount of downloaded pictures

The compression of the amount of downloaded image data has a significant effect on the performance improvement.SDWebImage/WebPThe compression rate of the picture is high, and no difference can be seen by the naked eye. As long as the third-party library is installed, we can use the image download as before.

2、 Data cache

For some data that does not need to be updated in real time, such as pictures and text, we can cache it locally, set an expiration time, and do not need to load it every time. This can not only save users’ traffic, but also improve the performance of our program.

For some time-consuming calculation results that do not need to be changed, we can cache them. such asRow height and time-consuming calculation results of dynamic cell

3、 Loading UI and data using lazload

Use lazload to load the UI and data.lazyloadcallLazy loadingperhapsDelayed loadingIt is to wait until the control or data is really needed to load, to ensure that the current control or data is loaded as much as possible, and to reduce the load task. Don’t put everything into VCdataandUI controlsAll of them are loaded, which will affect the loading speed and the user experience. thereforeWhether it's data or UI controls, we should only load them when we need to. We should spread the loading process to make the program load as fast as possible

4、 Use instruments to test app

After the completion of the development task in one stage, we use theInstrumentsTest the performance of our program, so as to optimize it.

The points to be optimized are as follows:

  1. UseLeaksCheck for memory leaks.
  2. UseCore AnimationCarry out layer detection. This article is very detailed.
  3. UseTime ProfileCheck whether there is a relatively time-consuming operation to optimize the corresponding code.

5、 Use of collections

In the development process, for some only used for storage and reading, no intermediateTo updateWe should try our best to use immutable sets, because variable sets will occupy more memory space than immutable sets. For example:NSArray/NSMutableArray,NSDictionary/NSMutableDictionary
When operating a collection, use the correct collection operation method.

6、 Use GCD for optimization

For some more time-consuming operations, we should use GCD to open a new step thread to operate, so that the app can run more smoothly and respond faster. However, when using GCD, attention should be paid to avoid thread explosion and deadlock, and non main thread processing tasks are not omnipotent. If a processing needs to consume a lot of memory or a lot of CPU operation, GCD cannot help you. It is more appropriate to separate the processing step by step and divide the processing time.

7、 I / O performance optimization

I / O is a large performance consumer. Any I / O operation will break the low power consumption state. Therefore, reducing the number of I / O is the key point of performance optimization. In order to achieve this goal, some methods are listed below.

  • Write fragmented content as a whole
  • Use appropriate I / O operation API
  • Use the right thread
  • Using nscache for caching can reduce I / O

8、 Reasonable use of autoreleasepool in memory consuming operation

For someLarge cycle or memory consuming cycle, we should use it inside the loopAutoRelasePoolTo release the memory, so that the memory can be recycled as soon as possible. Reduce the memory pressure of the system.

9、 Time consuming cache

During the development process, if similarDate format conversionWe should cache such operations because we use theinstrumentWhen we test its performance, we will find that this is a relatively time-consuming operation.
Actual case: in the previous development, we found that the operation of calculating ID is very time-consuming, and it affects the performance of the program very much. So we cache the calculated ID. in the later operation, we will not calculate any more and read the cache result directly. We found a significant improvement in performance.

10、 Algorithm optimization

In the early stage of development, we may just try to finish the development task as soon as possible in order to catch up with the project progress. When we look back at the code, we find that some algorithms are relatively time-consuming, and we can further optimize them, so that our program performance is better. Therefore, when the development task is not very heavy, we should check and optimize some algorithms written before.

11、 Installation package slimming

The size of the installation package has a certain impact on the performance of the program. In our continuous version iteration process, some unnecessary pictures and files are more or less generated. These files will be packed when we pack them. In case that the installation package becomes larger, on the other hand, the performance of our program will be affected, we should delete them. Please refer to another article for specific operation. Portal

Reference article: in depth analysis of IOS performance optimization