NSDateComponents 时间配置文件 [英] NSDateComponents Time Profile
问题描述
我的问题与下面代码片段中给出的 NSDateComponents 操作的时间配置文件有关.
My problem is to do with the Time Profile of NSDateComponents operations given in the code snippet below.
- 我有一个方法可以遍历大量 NSDate 对象
- 部分方法要求我丢弃每个日期的小时、分钟和秒.
为此,我有 3 个步骤:
- 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:
- 我从原始日期创建了一个 NSDateComponents 对象.
- 我将 H:M:S 元素设置为 0
- 我根据需要将 H:M:S 设置为 0 的新日期对象
问题:
步骤 1 &以上 3 个在我的整体方法执行时间中占据了很大比例,分别为 22.4% 和 56.8%.
Steps 1 & 3 above are taking a huge percentage of my overall method execution time, 22.4% and 56.8% respectively.
我正在寻找有关如何优化这部分代码的建议或将 H:M:S 归零的替代方法,这些方法可能会表现得更好.
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];
推荐答案
更新
时钟:
时间 1:≈ 0.000139 - 选项 1 - 原始
Time 1: ≈ 0.000139 - Option 1 - Original
时间 2:≈ 0.000108 - 选项 2
Time 2: ≈ 0.000108 - Option 2
时间 3:≈ 0.000013 - 选项 3
Time 3: ≈ 0.000013 - Option 3
时间 4:≈ 0.000004 - 选项 4
Time 4: ≈ 0.000004 - Option 4
NSDate* originDate = [[NSDate alloc] init];
NSDate* date = [[NSDate alloc] init];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents* components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit
| NSDayCalendarUnit | NSHourCalendarUnit
| NSSecondCalendarUnit) fromDate:date];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
date = [calendar dateFromComponents:components];
NSLog(@"Time 1: %f", -[originDate timeIntervalSinceNow]);
选项 2
你不需要
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
甚至不要将这些添加为日期组件的选项
Just don't even add these as options to date components
NSDate* originDate2 = [[NSDate alloc] init];
NSDate* date2 = [[NSDate alloc] init];
NSCalendar *calendar2 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents* components2 = [calendar2 components:(NSYearCalendarUnit | NSMonthCalendarUnit
| NSDayCalendarUnit
) fromDate:date2];
date2 = [calendar2 dateFromComponents:components2];
NSLog(@"Time 2: %f", -[originDate2 timeIntervalSinceNow]);
来源:类似问题 + David 的评论
Sources: Similar Question + David's Comment
基本上,我发现如果我使用已经创建的 NSCalendar 运行该方法,我的速度始终会提高 85% 到 90%.
Basically, I found that if I run the method with an already created NSCalendar, I get 85% to 90% faster speed consistently.
声明 calendarProperty 并在运行该方法之前对其进行初始化
Declare calendarProperty and initialize it before you run the method
NSDate* originDate3 = [[NSDate alloc] init];
NSDate* date3 = [[NSDate alloc] init];
NSDateComponents* components3 = [calendarProperty components:(NSYearCalendarUnit | NSMonthCalendarUnit
| NSDayCalendarUnit
) fromDate:date3];
date3 = [calendarProperty dateFromComponents:components3];
NSLog(@"Time 3: %f", -[originDate3 timeIntervalSinceNow]);
选项 4
* 基于@HotLicks 的评论 *
Option 4
* based on @HotLicks's comment *
同时避免所有那些烦人的 dateComponents - 大约快 95%
Avoid all those annoying dateComponents all together - about 95% faster
我不确定选项 4 在实践中的一致性如何,但它的计时速度最快,到目前为止对我来说是可靠的.
I'm not sure about how consistent Option 4 is in practice, but it's clocking in the fastest and it has been reliable for me so far.
NSDate* originDate4 = [[NSDate alloc] init];
NSDate* date4 = [[NSDate alloc] init];
float date4Interval = [date4 timeIntervalSince1970];
int totalDays = date4Interval / (60 * 60 * 24);
date4 = [NSDate dateWithTimeIntervalSince1970:totalDays * 60 * 60 * 24];
NSLog(@"Time 4: %f", -[originDate4 timeIntervalSinceNow]);
这篇关于NSDateComponents 时间配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!