ObjC Optimization Tips
Over the
past few weeks, I've been focus on doing performance tuning on a iPad app.
A little background of the app
It is an
business app, doing financial investment. It has lots of moving parts on the GUI, user can drag, slide and flipping, a graph, and lots of numbers and dates. User can play around with different combination of input and see their investment prediction over few years. There were around 50 parameters (or more) that affects actual calculation result. The app has at least 8000 lines of codes just for doing calculation.
The story
The Product Owner has been complaining to the team that the app has a serious performance issue. It's sluggish and the financial result does not reflect user's interaction immediately (PO generally don't care how complicate you back-end is). The financial graphs took ~5 seconds to redraw on screen, when user is moving the slider, and they are moving it continuously back and forth.
Tools I uses for this task.
I mainly
use Xcode's Instrument tools to analyst and tune the performance. It has been
proven to be an extremely useful tool set.
My Verdicts
The
following are the result of my findings. Some of this are pretty basic, but I
find some of our experience iOS developer still does it. So I think it's important to spell it out to remind everyone.
- NSDate, NSCalendar and NSDateComponents.
- NSDate is often used in-conjunction with NSCalendar and NSDateComponents, converting a date to string for display on view is unavoidable; though they provide more functionality they tends to have high tax on performance. Only use this when your need more then what the standard C type time_t can't provide what you need.
- If you do use the C type it, please test it properly because there are cases where the C type can't handle.
Tips:
Use C type time_t for heavy date related calculation. E.g. On a graph.
- NSNumberFormatter and NSLocale
- I use NSNumberFormatter and NSLocale to format numbers into da_DK locale (Danish locale)
Tips:
Create the object once and reuse especially on moving part of the GUI, e.g
Graph
- NSDateFormatter
- This is same as #2. Create once and store it in memory for reuse.
- NSDecimalNumber and NSNumber
- Boxing and unboxing NS data type are expensive. Don't do them in a loop. A way to do this is to make all the interrelated method use the same data type signature.
- NSString
- You can never avoid to perform operations on NSString in any type of programming language. Concatenating, splitting, substring or converting it to different data type is an expensive operation.
- NSMutableArray or NSArray 's KVO and NSPredicate or NSSortDescriptor
- KVO is convenient for doing summation, finding max or min in a collection; but it’s 10 times slower then a normal for loop. KVO itself is doing loop internally.
- NSSortDescriptor is another things you should be caution when using in a loop. They can be really slow as they are doing loops internally.
Last but not least
These are not all that causes the app be sluggish, but they are the main causes. If you look really closely you will notice deep down under
those methods mentioned above, they are mostly "string" manipulation or comparison related functions.
Happy coding.
Happy coding.
Comments
Post a Comment