使用 NSMutableDictionary 与 NSMutableArray 造成的性能损失>

发布于 2024-07-27 12:49:50 字数 152 浏览 12 评论 0原文

我正在考虑使用 NSMutableDictionary 代替我当前的 NSMutableArray。 这主要是出于 KVC/KVO 的原因。 该集合将在我的绘图方法的内循环中经历严重的变化。 如果我继续进行此替换,性能是否会受到重大影响?

干杯, 道格

I am considering using an NSMutableDictionary in place of my current NSMutableArray. This is primarily for KVC/KVO reasons. The collection will undergo heavy mutation within the inner loop of my drawing method. Can I expect to incur a significant performance hit if I go ahead with this replacement?

Cheers,
Doug

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

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

发布评论

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

评论(3

清风夜微凉 2024-08-03 12:49:50

唯一确定的方法就是测量。 我们没有人对 NSMutableDictionary 和 NSMutableArray 的实现如何工作有足够的了解,所以询问没有什么意义。

当然,您可能会遇到一些命中,因为字典必须进行简单数组不会进行的额外哈希处理。 这是否“重要”很难说。

再次测量

The only way to be sure is to measure. None of us have enough knowledge about how NSMutableDictionary's and NSMutableArray's implementations work, so there's little point asking.

Granted, you could probably expect some hit since the dictionary has to do additional hashing that a simple array would not. Whether or not that is "significant" is hard to say.

Again, measure.

甜柠檬 2024-08-03 12:49:50

正如他们所说,你需要测试这些东西。 但是......下面的简单测试对我有启发性,让我了解在小尺寸高分配率示例的情况下 NSMutableDictionary 和 NSMutableArray 集合类之间的相对速度差异。

运行以下程序的时间为:(打开垃圾收集)(在最近的四核机器上)

NSMutableDictionary 4.624478 秒
NSMutableArray 1.806365 秒

int main (int argc, const char * argv[])
{
    NSLog(@"Hello, World!");

    LNCStopwatch* stopwatch = [[LNCStopwatch alloc] init];
    [stopwatch start];
    for (int i = 1; i< 1000000; i++)
    {
        NSMutableDictionary* dict = [[NSMutableDictionary alloc]init];
        [dict setObject:@"a" forKey:@"a"];
        [dict setObject:@"b" forKey:@"b"];
        [dict setObject:@"c" forKey:@"c"];
        [dict setObject:@"d" forKey:@"d"];
        [dict setObject:@"e" forKey:@"e"];
        [dict setObject:@"y" forKey:@"a"];
        [dict setObject:@"x" forKey:@"d"];
    }
    [stopwatch stopAndLogTimeAndReset];
    [stopwatch start];
    for (int i = 1; i< 1000000; i++)
    {
        NSMutableArray* arr = [[NSMutableArray alloc]init];
        [arr addObject:@"a"];
        [arr addObject:@"b"];
        [arr addObject:@"c"];
        [arr addObject:@"d"];
        [arr addObject:@"e"];
        [arr replaceObjectAtIndex:[arr indexOfObject:@"a"] withObject:@"y"];
        [arr replaceObjectAtIndex:[arr indexOfObject:@"d"] withObject:@"x"];
    }
    [stopwatch stopAndLogTimeAndReset];

    return 0;
}

(我认为绝对时间并不是那么重要,它只是相对时间,对于这些小尺寸的类来说更重要。对于较大尺寸的类,当然集合类的性质将占主导地位,例如 NSMutableDictionary应该是 O(1) 来找到一个元素,等等......)

As they say you need to test these things. But... the following simple test was instructive for me to get an idea of the relative difference in speed between the NSMutableDictionary and NSMutableArray collection classes in the case of small size high allocation rate examples.

Running the following program the times were: (with garbage collection on) (on a recent quad core machine)

NSMutableDictionary 4.624478 seconds
NSMutableArray 1.806365 seconds

int main (int argc, const char * argv[])
{
    NSLog(@"Hello, World!");

    LNCStopwatch* stopwatch = [[LNCStopwatch alloc] init];
    [stopwatch start];
    for (int i = 1; i< 1000000; i++)
    {
        NSMutableDictionary* dict = [[NSMutableDictionary alloc]init];
        [dict setObject:@"a" forKey:@"a"];
        [dict setObject:@"b" forKey:@"b"];
        [dict setObject:@"c" forKey:@"c"];
        [dict setObject:@"d" forKey:@"d"];
        [dict setObject:@"e" forKey:@"e"];
        [dict setObject:@"y" forKey:@"a"];
        [dict setObject:@"x" forKey:@"d"];
    }
    [stopwatch stopAndLogTimeAndReset];
    [stopwatch start];
    for (int i = 1; i< 1000000; i++)
    {
        NSMutableArray* arr = [[NSMutableArray alloc]init];
        [arr addObject:@"a"];
        [arr addObject:@"b"];
        [arr addObject:@"c"];
        [arr addObject:@"d"];
        [arr addObject:@"e"];
        [arr replaceObjectAtIndex:[arr indexOfObject:@"a"] withObject:@"y"];
        [arr replaceObjectAtIndex:[arr indexOfObject:@"d"] withObject:@"x"];
    }
    [stopwatch stopAndLogTimeAndReset];

    return 0;
}

(The absolute times I don't think are really all that important, its just the relative times which are more important for these small size classes. For larger size classes of course the nature of the collection class will dominate, eg NSMutableDictionary should be O(1) to find an element, etc... )

素手挽清风 2024-08-03 12:49:50

当你说“主要是出于 KVC/KVO 原因”时,你能详细说明一下吗?

如果您因严重突变下过多的 KVO 触发而遇到性能问题,请考虑在完成后自行触发 KVO 通知:

[self willChangeValueForKey: @"myArray"];

// loop and mutate

[self didChangeValueForKey: @"myArray"];

When you say "primarily for KVC/KVO reasons", can you elaborate?

If you're seeing performance problems due to excessive KVO firing under heavy mutation, consider firing the KVO notifications yourself once you're done:

[self willChangeValueForKey: @"myArray"];

// loop and mutate

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