为什么分配或初始化NSDateFormatter被认为是“昂贵的”? [英] Why is allocating or initializing NSDateFormatter considered "expensive"?

查看:91
本文介绍了为什么分配或初始化NSDateFormatter被认为是“昂贵的”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当人们说这很贵时,人们的意思是什么?我为中间存储创建了许多瞬态对象的实例(NSString和NSDate是典型的)。我怎么知道我的程序使用NSDateFormatter是否过度了?

What do people mean when they say this is expensive? I create instances of a lot of transient objects just for intermediate storage (NSString and NSDate are typical ones). How do I know if my program use of NSDateFormatter is overdoing it?

到现在为止,我倾向于创建相当于单身的东西,但我倾向于封装它与其相关的一些其他对象,所以我可以使用自引用。

Until now, I have tended to create what amounts to a singleton, but my preference would be to encapsulate it into some of the other objects it is associated with so I can use self references.

没有运行性能测试,我正在寻找一个更好的经验法则理解为什么我应该或不应该这样做。

Short of running performance tests, I am looking for a better "rule-of-thumb" understanding of why I should or shouldn't do this.

推荐答案

当这样的事情被称为昂贵时,并不一定意味着你永远不应该这样做,它只是意味着避免这样做在需要尽快退出方法的情况下。例如,当iPhone 3G是最新的设备时,我正在编写一个带有 UITableView 的应用程序,该格式化的数字显示在每个单元格中(我可能会添加,这是回来的)当我是iOS开发的初学者时)。我的第一次尝试如下:

When something like this is referred to as expensive, it doesn't necessarily mean that you should never do it, it just means avoid doing it in situations when you need to get out of a method as quickly as possible. For example, back when the iPhone 3G was the latest device, I was writing an application with a UITableView that formatted numbers for display in each cell (I might add, this was back when I was a beginner at iOS development). My first attempt was the following:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *reuseIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier forIndexPath:indexPath];

    MyManagedObject *managedObject = [self.managedObjects objectAtIndex:indexPath.row];
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

    [cell.textLabel setText:[managedObject title]];
    [cell.detailTextLabel setText:[numberFormatter stringFromNumber:[managedObject amount]]];

    return cell;
}

此代码的滚动性能很糟糕。帧速率下降到大约15 FPS,因为每次 tableView:cellForRowAtIndexPath:命中时我都会分配一个新的 NSNumberFormatter

The scrolling performance of this code was terrible. The frame rate dropped to about 15 FPS because I was allocating a new NSNumberFormatter every time tableView:cellForRowAtIndexPath: was hit.

我通过将代码改为此来修复它:

I fixed it by changing the code to this:

- (NSNumberFormatter *)numberFormatter {

    if (_numberFormatter != nil) {
        return _numberFormatter;
    }

    _numberFormatter = [[NSNumberFormatter alloc] init];
    [_numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

    return _numberFormatter;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *reuseIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier forIndexPath:indexPath];

    MyManagedObject *managedObject = [self.managedObjects objectAtIndex:indexPath.row];
    NSNumberFormatter *numberFormatter = [self numberFormatter];

    [cell.textLabel setText:[managedObject title]];
    [cell.detailTextLabel setText:[numberFormatter stringFromNumber:[managedObject amount]]];

    return cell;
}

这里的差别是我懒得加载 NSNumberFormatter 进入一个ivar,以便每次运行 tableView:cellForRowAtIndexPath:不再分配新实例。这个简单的改变将滚动性能推回到大约60 FPS。

The difference here is that I've lazily loaded the NSNumberFormatter into an ivar, so that each run of tableView:cellForRowAtIndexPath: no longer allocates a new instance. This simple change pushed the scrolling performance back up to about 60 FPS.

这个具体的例子不再那么相关了,因为新的芯片能够处理分配在不影响滚动性能的情况下,尽可能提高效率总是更好。

This specific example isn't quite as relevant anymore, as the newer chips are capable of handling the allocation without affecting scrolling performance, but it's always better to be as efficient as possible.

这篇关于为什么分配或初始化NSDateFormatter被认为是“昂贵的”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆