I’d like to increment over days in a loop, so that it counts 2012-11-10, 2012-11-11, 2012-11-12, …
What’s the most performant way to achieve this?
NSDate *iterationDate = [NSDate date];
for (int i = 0; i < 100; i++) {
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:0];
[comps setMonth:0];
[comps setWeek:0];
[comps setDay:1];
[comps setHour:0];
[comps setMinute:0];
[comps setSecond:0];
iterationDate = [currentCalendar dateByAddingComponents:comps toDate:iterationDate options:0];
}
If you need all the intermediate
NSDates, just pullcompsout of the loop:You can achieve something similar using CoreFoundation APIs:
That measured out to be 33% faster than Foundation. It’s even faster if you don’t need to create the CFDates and can simply store the
CFAbsoluteTimevalues.