有没有更好的方法来计算时间段?

发布于 2024-11-17 12:28:51 字数 1468 浏览 4 评论 0原文

我有以下代码。这是在 cellForRowAtIndexPath 中计算的,所以我认为它可能有点贵。有没有更好的方法来计算时间段?

+(NSString*)toShortTimeIntervalString:(NSString*)sDate  
{ 
    NSDateFormatter* df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
    NSDate* date = [df dateFromString:[sDate stringByReplacingOccurrencesOfString:@"Z" withString:@"-0000"]];
    [df release];

    NSDate* today = [[NSDate alloc] init];
    NSDate *d = date; //[_twitter_dateFormatter dateFromString:sDate];
    NSTimeInterval interval = [today timeIntervalSinceDate:d];
    [today release];

    //TODO: added ABS wrapper
    double res = 0;
    NSString* result;
    if(interval > SECONDS_IN_WEEK)
    {
        res = fabs(interval / SECONDS_IN_WEEK);
        result = [NSString stringWithFormat:@"%1.0fw ago", res];
    }
    else if(interval > SECONDS_IN_DAY)
    {
        res = fabs(interval / SECONDS_IN_DAY);
        result = [NSString stringWithFormat:@"%1.0fd ago", res];
    }
    else if (interval > SECONDS_IN_HOUR){
        res = fabs(interval / SECONDS_IN_HOUR);
        result = [NSString stringWithFormat:@"%1.0fh ago", res];
    }
    else if (interval > SECONDS_IN_MIN) {
        res = fabs(interval / SECONDS_IN_MIN);
        result = [NSString stringWithFormat:@"%1.0fm ago", res];
    }
    else
    {
        interval = fabs(interval);
        result = [NSString stringWithFormat:@"%1.0fs ago", interval];
    }
    return result;
}

I have the following code. This is getting calculated in cellForRowAtIndexPath, so I think it could be a little expensive. Is there a better way to calculate a time period?

+(NSString*)toShortTimeIntervalString:(NSString*)sDate  
{ 
    NSDateFormatter* df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
    NSDate* date = [df dateFromString:[sDate stringByReplacingOccurrencesOfString:@"Z" withString:@"-0000"]];
    [df release];

    NSDate* today = [[NSDate alloc] init];
    NSDate *d = date; //[_twitter_dateFormatter dateFromString:sDate];
    NSTimeInterval interval = [today timeIntervalSinceDate:d];
    [today release];

    //TODO: added ABS wrapper
    double res = 0;
    NSString* result;
    if(interval > SECONDS_IN_WEEK)
    {
        res = fabs(interval / SECONDS_IN_WEEK);
        result = [NSString stringWithFormat:@"%1.0fw ago", res];
    }
    else if(interval > SECONDS_IN_DAY)
    {
        res = fabs(interval / SECONDS_IN_DAY);
        result = [NSString stringWithFormat:@"%1.0fd ago", res];
    }
    else if (interval > SECONDS_IN_HOUR){
        res = fabs(interval / SECONDS_IN_HOUR);
        result = [NSString stringWithFormat:@"%1.0fh ago", res];
    }
    else if (interval > SECONDS_IN_MIN) {
        res = fabs(interval / SECONDS_IN_MIN);
        result = [NSString stringWithFormat:@"%1.0fm ago", res];
    }
    else
    {
        interval = fabs(interval);
        result = [NSString stringWithFormat:@"%1.0fs ago", interval];
    }
    return result;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜夜流光相皎洁 2024-11-24 12:28:51

这看起来并不算太糟糕,而且我怀疑您会看到它对性能造成太大影响。为了提高效率,您可以考虑仅创建一个 NSDataFormatter(将其保存在实例变量或静态变量中)并重用它。或者更好的是,如果您可以事先将所有内容转换为 NSDates,那么您就不必每次都使用格式化程序。

不过,您实际上在这里看到任何性能问题吗?在尝试优化之前,您应该使用 Instruments 来调查实际占用时间的因素。

This doesn't look too bad, and I doubt you'll be seeing too much of a performance hit from it. To make it more efficient, you might consider only creating one NSDataFormatter (saving it in an instance variable or static variable) and reusing it. Or even better, if you could convert everything to NSDates beforehand, then you wouldn't have to use the formatter every time.

Are you actually seeing any performance issues here, though? Before you try and optimize, you should use Instruments to investigate what's actually taking up time.

匿名的好友 2024-11-24 12:28:51

不是真的,但是有一个 几个类别 封装了该逻辑并使其成为一行打电话给你。

Not really, but there are a few categories out there which encapsulate that logic and make it a one line call for you.

生生不灭 2024-11-24 12:28:51

最近,我遇到了执行复杂计算(多年来绘制图表)的情况,并且我在循环中使用 NSDate 。经过时间分析器NSDate、NSDateFormater、NSCalendar分析,问题的根源在于NSDate、NSCalendar。我改用 C 类型(time_t + timeinfo),它极大地提高了速度。

虽然它们速度更快,但功能较少,如果您需要处理 TimeZone 等,NSDate 可能更容易处理。

Recently I've been in a situation to perform complex calculation (plotting graph for series of years), and I was using NSDate within loops. After analyze with time profiler NSDate, NSDateFormater, NSCalendar was the root cause. I switched to use the C type (time_t + timeinfo) and it drastically improves the speed.

Although they are much faster, they have less functionality, if you need to deal with TimeZone etc, NSDate is probably easier to deal with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文