使用对象和键迭代 NSArray 的最快方法

发布于 2024-08-27 08:28:01 字数 332 浏览 1 评论 0原文

我有一个名为“objects”的 NSArray,其 arrayCount = 1000。迭代该数组大约需要 10 秒。有谁有更快的方法来迭代这个数组?

for (int i = 0; i <= arrayCount; i++) {
    event.latitude = [[[objects valueForKey:@"CLatitude"] objectAtIndex:i] floatValue];
    event.longitude = [[[objects valueForKey:@"CLongitude"] objectAtIndex:i] floatValue];
}

I have an NSArray called 'objects' below with arrayCount = 1000. It takes about 10 secs to iterate through this array. Does anyone have a faster method of iterating through this array?

for (int i = 0; i <= arrayCount; i++) {
    event.latitude = [[[objects valueForKey:@"CLatitude"] objectAtIndex:i] floatValue];
    event.longitude = [[[objects valueForKey:@"CLongitude"] objectAtIndex:i] floatValue];
}

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

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

发布评论

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

评论(3

池予 2024-09-03 08:28:01

迭代 NSArray 的最快方法是使用 快速枚举。我的情况是:

for (id object in objects) {
    event.latitude = [[object valueForKey:@"CLatitude"] floatValue];
    event.longitude = [[object valueForKey:@"CLongitude"] floatValue]
}

The fastest way to iterate over an NSArray would be to use fast enumeration. I your case this would be:

for (id object in objects) {
    event.latitude = [[object valueForKey:@"CLatitude"] floatValue];
    event.longitude = [[object valueForKey:@"CLongitude"] floatValue]
}
幽梦紫曦~ 2024-09-03 08:28:01

看起来 valueForKey 返回的数组在每次迭代中都是相同的,因此尝试在循环外获取对它们的引用:

NSArray *latitudes = [objects valueForKey:@"CLatitude"];
NSArray *longitudes = [objects valueForKey:@"CLongitude"];
for (int i = 0; i <= arrayCount; i++) {
    event.latitude = [[latitudes objectAtIndex:i] floatValue];
    event.longitude = [[longitudes objectAtIndex:i] floatValue];
}

但是循环的意义何在?最后,它不只是将事件属性设置为两个数组中的最后一个值吗?另外,如果 arrayCount 是元素数量,则循环应达到 i i i i i i i i i i i i i i i arrayCount 而不是 i <= arrayCount

It looks like the arrays returned by valueForKey will be the same at every iteration so try obtaining a reference to them once outside the loop:

NSArray *latitudes = [objects valueForKey:@"CLatitude"];
NSArray *longitudes = [objects valueForKey:@"CLongitude"];
for (int i = 0; i <= arrayCount; i++) {
    event.latitude = [[latitudes objectAtIndex:i] floatValue];
    event.longitude = [[longitudes objectAtIndex:i] floatValue];
}

But what is the point of the loop? In the end, doesn't it just set the event properties to the last values in the two arrays? Also, if arrayCount is the number of elements then the loop should be up to i < arrayCount instead of i <= arrayCount.

李白 2024-09-03 08:28:01

添加到这里其他人所说的,快速枚举更快,因为它迭代 C 数组,而使用 objectAtIndex: 访问索引具有 Objective C 对象消息传递的开销。可以将其视为立即向 NSArray 对象请求其所有内容,而不是单独请求每个对象。

enumerateObjectsUsingBlock: 方法几乎同样快。块的功能非常强大,因为您可以将一段代码分配给一个变量并将其传递。

我正在寻找实际的基准测试并发现了这个:

http://darkdust.net/writings /objective-c/nsarray-枚举-性能

Adding to what others have said here, fast enumeration is faster because it iterates a C array, whereas accessing indices using objectAtIndex: has the overhead of Objective C object messaging. Think of it as asking the NSArray object for all of its stuff at once, as opposed to asking for each object individually.

The enumerateObjectsUsingBlock: method is nearly just as fast. Blocks can be powerful because you can assign a chunk of code to a variable and pass it around.

I was looking for actual benchmarks and found this:

http://darkdust.net/writings/objective-c/nsarray-enumeration-performance

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