ios-Performance Optimization 1

Source: Internet
Author: User

iOS app is very focused on user experience, not only requires the interface design is reasonable and beautiful, but also requires a variety of UI responsiveness, I believe we are the kind of a drag on Kaka TableView application is not a good impression. Remember 12306, that speed, I believe we can not stand it. In order to improve the speed of iOS, I would like to introduce some of the methods I have practiced to provide the efficiency of iOS programs, to share with you, hoping to get more feedback and suggestions. 1, calculate the code run time: Believe the data, do not believe too much feeling. But be aware of the difference between the simulator and the real machine. The simplest tool is nsdate, but the accuracy is not too good.
nsdate* tmpstartdata = [[nsdate Date] retain];//you code here...double deltatime = [[NSDate Date] Timeintervalsincedate:tm Pstartdata]; NSLog (@ ">>>>>>>>>>cost time =%f", deltatime);

Or put the running code in the block parameter of the following method, and return the elapsed time:

#import <mach/mach_time.h>  //For Mach_absolute_time () and Friends    cgfloat bnrtimeblock (void (^block) ( void)) {      mach_timebase_info_data_t info;      if (Mach_timebase_info (&info)! = kern_success) return-1.0;        uint64_t start = Mach_absolute_time ();      Block ();      uint64_t end = Mach_absolute_time ();      uint64_t elapsed = End-start;        uint64_t Nanos = elapsed * INFO.NUMER/INFO.DENOM;      Return (cgfloat) nanos/nsec_per_sec;  

2. Use the Performance analysis tool.

XCode comes with a number of powerful analysis tools, including static Analyze tools, and runtime profile tools.

3, about the picture

Preferential use [UIImage imagenamed:@ "];

Compared to [[UIImage alloc] Initwithcontentsoffile:] and [UIImage alloc [Initwithdata:]], [UIImage imagenamed:] has better efficiency because IOS will bring the cache through the [UIImage imagenamed:] loaded image, but the method has a disadvantage, that is, can only load the application bundle of images, such as the network download image is powerless. I am accustomed to the practice is to customize a Imagecache class, to cache images on their own.

Try not to use a full-screen-sized background image, or use gradient images instead of hard-coded gradient;gradient images to be as narrow as possible and then stretch them into practical situations.

4, for a complex View, use DrawRect to draw instead of loading from the nib.

5, for TableView, reuse cell, reduce the workload of cell initialization, delay load, customize complex cell, use DrawRect to paint, Cache as many things as possible, including cell height, make cell opaque, and avoid using image features such as Gradients.

6, use Autoreleasepool in thread.

7, put some less important tasks to run when idle.

-(void) Idlenotificationmethod {    //do something here}-(void) registerforidlenotification {    [[ Nsnotificationcenter Defaultcenter] addobserver:self        selector: @selector (idlenotificationmethod)        name:@ " Idlenotification "        Object:nil";    Nsnotification *notification = [nsnotification        notificationwithname:@ "Idlenotification" object:nil];    [[Nsnotificationqueue Defaultqueue] Enqueuenotification:notification    

8, do not take the time-consuming operation in the viewwillappear.

Viewwillappear: Called before the view display, for efficiency reasons, do not deal with complex and time-consuming things in this method, only should be in this method to set the view display properties such as simple things, such as background color, font and so on. Otherwise, the user will obviously feel the view display is dull.

9, use multithreading to delay loading resources. For example, the common Tableviewcell in the network image display, first use a default image, and then open the thread to download the network image, when the image download is complete, then replace the default image.

10, about background tasks

After the system enters background, generally only 10 minutes of running time, so there are a lot of noteworthy matters:

    • A) Minimize the use of memory. When memory is low, iOS will kill those apps that consume the most memory.
    • b) Release all shared resources, such as Calendar and Address book. When the application enters the background, if it is still in use or does not release the shared resource, iOS will kill the application immediately.
    • c) correctly handle app life cycle events. When you enter the background, you should keep your application data so that you can recover when you return to the foreground. When entering the inactive state, the current business flow should be paused. iOS runs the app in the background with limited time, so background code should not perform a very time-consuming task and use multithreading if possible. When entering the background, iOS saves a snapshot of the current app so that it can be presented to the user at the right time (loading the view and data) to improve the user experience, so you should avoid presenting user information on the screen when you enter the background to avoid revealing user profiles.
    • d) Do not update the UI or execute large amounts of code that consumes CPU or battery. After entering the background, you should not perform unnecessary tasks, do not perform OpenGL ES calls, cancel Bonjour related services, properly handle network link failures, avoid updating the UI, clear all warnings, or other pop-up dialogs.
    • e) To ensure that the execution of the background code is working properly, pay attention to handling exceptions.
    • f) respond correctly to system changes in the background. such as: Device rotation message uideviceorientationdidchangenotification, Important time changes (New Day start or time zone change) uiapplicationsignificanttimechangenotification, Battery changes uidevicebatteryleveldidchangenotification and uidevicebatterystatedidchangenotification, User default settings change nsuserdefaultsdidchangenotification, localization language changes nscurrentlocaledidchangenotification, etc.
11, if the key code uses C/C++/ASM more efficient use of c/c++/asm. 12, if a method is used in a loop of very many cycles, use Methodforselector to obtain the method imp before entering the loop, and then use the IMP directly in the loop body. 13, about memory release in didreceivememorywarning release memory, such as cache image, view, etc., and remember to call [supper didreceivememorywarning]. Cleanup functions didreceivememorywarning, viewdidunload, and dealloc are methods that call supper at the end of the method. 14, improve APP loading speed avoid static initialization, including static C + + objects, code that runs when loaded, such as + (void) load{}, causes additional code to run before the main function. 16. Use the cache space to change time. Cache is a common space-time-to-deliver performance that can be used on quite a number of occasions. Try to cache those reusable objects, such as table cell,date/number formatters, regular expressions, sqlite statements, and so on. 17, about the database cache frequently used SQLite statements, optimized database query statements, using Sqlite3_trace and Sqlite3_profile to find poor performance statements, if possible, cache query results slow. When using Sqlite_prepare, SQL queries are compiled into bytecode, using bind to reuse those statements that have been prepared.

ios-Performance Optimization 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.