Search code examples
cocoansstringnsdatenscalendarnsdatecomponents

stringWithFormat or stringFromDate


Simple / basic question:

If I do not care to format the date being obtained, what are the performance differences between the two?

 NSDate *date = [NSDate date];
 NSSting *dateString = [NSString stringWithFormat:"%@", date];

&&

 NSDate *date = [NSDate date];
 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
 [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];

Sorry for such a simple question, I just am not finding what I am looking for searching google and can't find a question on here that addresses it.

The three objects below seem very expensive when working with a large list of CFDateRefs and I am attempting to create a work around.

Any advice on that would be awesome too, but I am more focused on the question at hand.

  • NSDateComponenets
  • NSCalendar
  • NSDateFormatter

thanks!


Solution

  • Those will likely have similar performance (the -description method that stringWithFormat calls will use a date formatter internally).

    If you create the date formatter once, then use it for many dates, you'll get much much better performance.