Skip to content

Commit

Permalink
Moved the calendar and displayFormatter into static variables and ini…
Browse files Browse the repository at this point in the history
…tialized them in the +load method.

When you've got to format a lot of dates, this greatly improves performance. Getting the shared calender and init'ing a new displayFormatter are both expensive operations.
  • Loading branch information
jkubicek committed Apr 12, 2011
1 parent 7288934 commit 8e80c1a
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions NSDate+Helper.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,26 @@

#import "NSDate+Helper.h"

static NSCalendar *calendar;
static NSDateFormatter *displayFormatter;

@implementation NSDate (Helper)

+ (void)load {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

calendar = [[NSCalendar currentCalendar] retain];
displayFormatter = [[NSDateFormatter alloc] init];

[pool drain];
}

/*
* This guy can be a little unreliable and produce unexpected results,
* you're better off using daysAgoAgainstMidnight
*/
- (NSUInteger)daysAgo {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSDayCalendarUnit)
NSDateComponents *components = [calendar components:(NSDayCalendarUnit)
fromDate:self
toDate:[NSDate date]
options:0];
Expand Down Expand Up @@ -76,8 +87,7 @@ - (NSString *)stringDaysAgoAgainstMidnight:(BOOL)flag {
}

- (NSUInteger)weekday {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *weekdayComponents = [calendar components:(NSWeekdayCalendarUnit) fromDate:self];
NSDateComponents *weekdayComponents = [calendar components:(NSWeekdayCalendarUnit) fromDate:self];
return [weekdayComponents weekday];
}

Expand Down Expand Up @@ -110,13 +120,11 @@ + (NSString *)stringForDisplayFromDate:(NSDate *)date prefixed:(BOOL)prefixed {
*/

NSDate *today = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *offsetComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
NSDateComponents *offsetComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate:today];

NSDate *midnight = [calendar dateFromComponents:offsetComponents];

NSDateFormatter *displayFormatter = [[NSDateFormatter alloc] init];
NSString *displayString = nil;

// comparing against midnight
Expand Down Expand Up @@ -156,7 +164,6 @@ + (NSString *)stringForDisplayFromDate:(NSDate *)date prefixed:(BOOL)prefixed {

// use display formatter to return formatted date string
displayString = [displayFormatter stringFromDate:date];
[displayFormatter release];
return displayString;
}

Expand Down Expand Up @@ -189,8 +196,7 @@ - (NSDate *)beginningOfWeek {
// largely borrowed from "Date and Time Programming Guide for Cocoa"
// we'll use the default calendar and hope for the best

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *beginningOfWeek = nil;
NSDate *beginningOfWeek = nil;
BOOL ok = [calendar rangeOfUnit:NSWeekCalendarUnit startDate:&beginningOfWeek
interval:NULL forDate:self];
if (ok) {
Expand Down Expand Up @@ -218,16 +224,14 @@ - (NSDate *)beginningOfWeek {
}

- (NSDate *)beginningOfDay {
NSCalendar *calendar = [NSCalendar currentCalendar];
// Get the weekday component of the current date
// Get the weekday component of the current date
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate:self];
return [calendar dateFromComponents:components];
}

- (NSDate *)endOfWeek {
NSCalendar *calendar = [NSCalendar currentCalendar];
// Get the weekday component of the current date
// Get the weekday component of the current date
NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:self];
NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
// to get the end of week for a particular date, add (7 - weekday) days
Expand Down

3 comments on commit 8e80c1a

@alanb1501
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think using Dependency Injection would allow you to increase performance, while still allowing for thread safety?

@billymeltdown
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I'm not quite sure! Sorry that I haven't responded in so long, I've just been a bit busy. If you've got some spare cycles and you want to give it a shot, I'm more than happy to accept pull requests and try it out. I had some minor correspondence with Mike Ash about this trouble over here in the comments, and he suggested what sounds like a very simple fix (but I just haven't tried it out yet):

My recommendation would be to use dispatch_once to lazily initialize your static variable. The additional overhead is insignificant and you completely avoid problems like this.

@jkubicek
Copy link
Contributor Author

@jkubicek jkubicek commented on 8e80c1a May 7, 2012 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.