-5
- (NSString *)formattedDate {
    static NSDateFormatter *dateFormatter = nil;

    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd.MM.yyyy"];
    }

    NSDate *today = [NSDate date];
    NSString *formattedDate = [dateFormatter stringFromDate:today];

    return formattedDate;
}

and

- (NSString *)formattedDate {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd.MM.yyyy"];

    NSDate *today = [NSDate date];
    NSString *formattedDate = [dateFormatter stringFromDate:today];

    return formattedDate;
}

What is the difference between both and which one is better?

NARKOZ
  • 27,203
  • 7
  • 68
  • 90

3 Answers3

2

The first one has this at the start:

static NSDateFormatter *dateFormatter = nil;

if (dateFormatter == nil) {
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd.MM.yyyy"];
}

... so it does that.

The second one has this:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd.MM.yyyy"];

... so it does that instead.

I'm here all week.

Let's look at what the second one is doing first. It's creating a date formatter, and telling it what format dates it wants. It then gets that date formatter to format the current date. It does all those steps every time.

The second one creates a date formatter, but only if it hasn't already done so. It hangs onto the date formatter by putting it in a static variable. It then gets the formatter to format the current date

If they only run once, they're pretty much the same; the first will be infinitesimally slower because it has to do the check. The difference comes if they run lots of times. The first one runs like this:

Create a date formatter
Format the date
Create a date formatter
Format the date
Create a date formatter
Format the date
...

The second one runs like this:

Check for a date formatter
Create a date formatter
Format the date
Check for a date formatter
Format the date
Check for a date formatter
Format the date
...

Creating a date formatter is a very expensive operation, but checking for one isn't, so if the function is called lots of times, the second version is much faster.

Simon
  • 25,468
  • 44
  • 152
  • 266
1

The first example uses static variable and initializes it only ones. The second one initializes dateFormater everytime. So the performance of the first example is a little bit better.

Sviatoslav Yakymiv
  • 7,887
  • 2
  • 23
  • 43
1

When you dispatch [self formattedDate],

  • The first one will reuse the dateFormatter (recommend);
  • The second one will always allocate a new NSDateFormatter instance for you to use.
Kjuly
  • 34,476
  • 22
  • 104
  • 118
  • if I remove `static NSDateFormatter *dateFormatter = nil;` from the first code will it still reuse `dateFormatter`? – NARKOZ Dec 27 '13 at 13:43
  • @NARKOZ of course not unless you declared it as an iVar. And also, most of the singletone patterns use this strategy, you can take a look at http://stackoverflow.com/questions/7598820/correct-singleton-pattern-objective-c-ios and http://www.galloway.me.uk/tutorials/singleton-classes/ if you're interested in. :) – Kjuly Dec 27 '13 at 14:11