My problem is to do with the Time Profile of NSDateComponents operations given in the code snippet below.
- I have a method that iterates over a large array of NSDate objects
- Part of the method requires that I discard the Hour, Minute and seconds of each date.
To do this I have 3 steps:
- I create an NSDateComponents object from my original date.
- I set the H:M:S elements to 0
- I get my new date object that has H:M:S set to 0 as needed
The Issue:
Steps 1 & 3 above are taking a huge percentage of my overall method execution time, 22.4% and 56.8% respectively.
I'm looking for suggestions as to how I might optimize this part of my code or alternative ways of zeroing the H:M:S that might perform better.
NSDate* date = [[NSDate alloc] init];
// Next line takes 22.4% of the overall method execution time
NSDateComponents* components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit
| NSDayCalendarUnit | NSHourCalendarUnit
| NSSecondCalendarUnit) fromDate:date];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
// Next line takes 56.8% of the overall method execution time
date = [calendar dateFromComponents:components];