3

Is there a difference between these two and which one may be better or faster or have any advantage between:

UIAlertView * alertBox = [[UIAlertView alloc] initWithTitle:@"Title"];

And:

UIAlertView * alertBox = [[UIAlertView alloc] init];
alertBox.title = @"Title";

(They both display the same result of course!)

  • 1
    micro optimization at its best – peko Jan 08 '13 at 15:41
  • @peko I can't help it, I just want to prevent future errors or slowness by start optimizing from the very beginning as detailed as possible. –  Oct 17 '13 at 11:24

4 Answers4

2
UIAlertView * alertBox = [[UIAlertView alloc] initWithTitle:@"Title"];

Uses the init method which is similar to constructor, which internally calls .title method.

UIAlertView * alertBox = [[UIAlertView alloc] init];    
alertBox.title = @"Title";

First statement creates an object alertBox with title as nil. In the second line you supply yours own title.

For performance :

2nd one will create another stack to perform the second statement, so two extra cpu processing push and pop. There for first one will be faster but with multicore processors and the time of calling is really negotiable.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • Though wouldn't that second group result in more lines when compiled? I'd assume that it would default to a title of "" at initialization. – CBredlow Jan 08 '13 at 13:55
  • `.title` is not a method, it is a declared property. It's true that assigning to it is the equivalent of calling a setter method, but that setter method is `setTitle:`. `title` would be the getter method. – Jim Jan 08 '13 at 16:42
  • @Jim : Yes i agree, i am still thinking for accessing title setTitle method will be called, isn't it? – Anoop Vaidya Jan 08 '13 at 16:44
  • There's also no guarantee that the initialiser assigns to the property. In fact, lots of coding standards explicitly ban the use of declared properties in initialisers. For all you know, it could simply be assigning to the backing instance variable, which is more efficient. – Jim Jan 08 '13 at 16:44
2

There is not really a difference in performance, one is setting the title on initialization, which really just calls the line of code alertBox.title in the custom initialization method. The reason they have the .title property is so you can change it.

So this code:

UIAlertView * alertBox = [[UIAlertView alloc] initWithTitle:@"Title"];

Would be better vs this code:

UIAlertView * alertBox = [[UIAlertView alloc] init];
alertBox.title = @"Title";

Just really because of the number of lines, but you can just use this code:

alertBox.title = @"New Title";

Later on to change it


If there is a difference in speed, you would need a planck second calculator to measure it :) - Some good hyperbole there, but basically no

For memory, there isn't a difference because you are intializing an object and setting it's title parameter in both cases, just separate ways of doing it. Think of the first one, as a shorter way for you to write it, but the actual class will basically do the same thing.

Using the constructer custom initializer method is more efficient to your time and number of lines, but to nothing else -- my verdict


Which one is more to an advantage? Neither, because you are using an alert view, I hate them!

MCKapur
  • 9,127
  • 9
  • 58
  • 101
  • lol the alert view was just an example, I was working with other objects like buttons (because I code my interface programmatically, no xib files for me) so that's why I asked this question –  Jan 08 '13 at 13:54
  • @YassineHoussni excuse my hate humor, it means nothing :) If the answer helped, feel free to upvote and tick – MCKapur Jan 08 '13 at 13:55
2

The former is probably slightly faster, however if you are optimising things like this, you are making a mistake. Showing an alert is something you do infrequently and immediately afterwards, you'll be hanging around waiting for the user to respond. There's no benefit in shaving a couple of microseconds off that, it will make absolutely no perceptible difference to how the application runs. Use whichever is clearer for you to read as a developer.

Jim
  • 72,985
  • 14
  • 101
  • 108
0

That first line is just less code for you to write out, both do the same thing, and are constructors for a UIAlertView. It's easier to do the constructor from the first line in my opinion because it's all there in one line instead of going through and adding each property in later.

CBredlow
  • 2,790
  • 2
  • 28
  • 47