0

I understand that objects may perform more effectively if you manually allocate and initialize them (ARC should have taken care of the majority of this, right?), however, is it really THAT BIG of a difference in performance if you were to just use convenience class methods every time to create objects?

Examples:

NSString *message = [[NSSTring alloc] initWithFormat:@"text %@", message];

vs

NSString *message = [NSString stringWithFormat:@"text %@", message];
giuseppe
  • 17
  • 4
  • This used to have a bigger impact because of autoreleasing objects from convenience constructors. Autorelease is not super cheap. However, ARC is smart about that and if it is not mixed with MRC then there's little chance objects from convenience initializers are added to the autorelease pool. – Bartek Chlebek Oct 27 '14 at 17:00
  • Also note that the convenience method implementation simply calls `alloc/init` to there is the (trivial) extra overhead of the additional method call. But don't even consider which of the two is better unless you actually run into a performance issue that needs to be addressed in your app. – rmaddy Oct 27 '14 at 17:11
  • Thank-you. This all makes sense. Basically, it boils down to readability, from what I now understand. – giuseppe Oct 27 '14 at 17:16
  • @giuseppe also remember, that some convenience methods, have extra features. For example `-[UIImage imageNamed:]` is unlike any other, because it has a caching mechanism under the hood, so most of the time it will be way faster than any other constructor. – Bartek Chlebek Oct 27 '14 at 17:18

2 Answers2

2

There is no meaningful difference in performance.

Even being concerned about that is a premature optimization and as Donald Knuth states: "Premature optimization is the root of all evil (or at least most of it) in programming."

Code clarity is a much more important issue.

zaph
  • 111,848
  • 21
  • 189
  • 228
0

I would suggest using alloc. This is not because of any real performance advantages and more for consistency sake.

You can use alloc to initialize any object, where as not all objects can provide you a class method. Also alloc can be a visual indicator of an object being created.

I personally like to be consistent through out my code. And as Zaph mentioned, "Code clarity is a much more important issue."

Reza Shirazian
  • 2,303
  • 1
  • 22
  • 30