Reputation: 22633
With regards to Objective-C running on iOS devices:
In an effort to optimize battery consumption (and therefore minimize clock-cycle usage), I'm concerned with the amount of effort it takes an iOS device to create and store a struct vs. the amount it takes to create and store an object.
Have objects been magically optimized such that the difference is negligible, or is there overhead associated with creating an object?
Upvotes: 1
Views: 143
Reputation: 10645
There is necessarily more work allocing and initializing an Objective C object than a C struct. Since there are so many objects created by iOS and apps running on iOS Apple has optimized the process very well. In most cases the difference is dwarfed by the rest of the processing work done in an app.
If you are dealing with a very large number of objects that can be represented by simple structs instead of objects, allocating structs could be noticeably more efficient than allocating and initializing objects.
If you are trying to recreate functionality of the Objective C runtime to avoid using objects instead of structs, your code is most likely less efficient than Apple's optimized Objective C runtime. You'll most likely trade more efficient storage allocation for less efficient data processing.
My advice is not to worry about object creation overhead. To meet your expressed goal, battery life extension, you're better off taking the advice of Caleb. Profiling the app to see where it works hardest, and focusing effort on improving those areas will see more benefit than trying to work against the framework by avoiding objects.
Upvotes: 1
Reputation: 124997
I doubt that any optimization in this area was of the magical variety, but given the importance of battery life to the quality of the user experience, it's a good guess that Apple has taken care of it. You'll have a far greater impact on battery life by following common sense guidelines:
Be lazy: don't do a lot of work that the user may never need.
Don't use network and GPS unnecessarily: radios consume a lot of power.
Be a good citizen: work with the framework instead of against it when it comes to things like letting your app be suspended.
Profile: use Instruments to profile your app's battery consumption.
Upvotes: 2