- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这段代码是一个创建数组供多个其他类使用的方法。输入是一个来自 CoreData 获取的数组,类型为 NSDictionaryResultType。
其中 3 个字段是我需要分成数组的字符串,因此是 componentsSeparatedByString。
生成的数组 _dataProductionArray 效果很好 --- 但是 --- 这段代码需要整整 5 秒来处理大约 32,000 条记录。
如果您能指出导致性能缓慢的明显错误,我们将不胜感激!!
NSMutableArray *dataArray = [NSMutableArray array];
int j = 0;
int maxNumMonths = 0;
for (id obj in _dictionaries) {
if ([_dictionaries[j] [@"month"] length] >0 ) {
// get production values
NSArray *aItems = [_dictionaries[j] [@"prodA"] componentsSeparatedByString:@","];
NSArray *bItems = [_dictionaries[j] [@"prodB"] componentsSeparatedByString:@","];
NSArray *monthItems = [_dictionaries[j] [@"month"] componentsSeparatedByString:@","];
NSMutableArray *productionAArray = [NSMutableArray array];
NSMutableArray *productionBArray = [NSMutableArray array];
int monthLoop = 1;
for (NSNumber *month in monthItems) {
if (monthLoop <= MONTHS_OF_PRODUCTION) {
if ([month intValue] == monthLoop) {
[productionAArray addObject:[aItems objectAtIndex:monthLoop-1]];
[productionBArray addObject:[bItems objectAtIndex:monthLoop-1]];
productionCount ++;
if (monthLoop > maxNumMonths)
maxNumMonths = monthLoop;
}
}
monthLoop++;
}
NSDictionary *arrayItem = @{@"name":_dictionaries[j] [@"name"],
@"type":[NSString stringWithFormat:@"%@",_dictionaries[j] [@"type"]],
@"height":[NSString stringWithFormat:@"%@",_dictionaries[j] [@"height"]],
@"aArray":productionAArray,
@"bArray":productionBArray,
};
[dataArray addObject:arrayItem];
}
j++;
}
_dataProductionArray = [NSArray arrayWithArray:dataArray];
最佳答案
我可以看到您可以在循环中进行一些优化,但我不确定这些有多大帮助(特别是如果编译器正在执行它们)。根本问题是 32k 是很多迭代。
您是否一次需要所有 32k 个结果?通过完成这项工作,您可以显着改善用户体验 lazily ,因为 UI 需要转换后的记录。
这种方法将使 dataProductionArray 成为一个可变字典,由 NSNumber 索引索引。然后,而不是...
// replace this
self.dataProductionArray[128];
// with this
[self dataProductionAtIndex:@128];
那个新的 getter 方法会调用你懒惰编写的代码,就像这样......
- (id)dataProductionAtIndex:(NSNumber *)index {
// replace dataProductionArray with dataProductionDictionary
id result = self.dataProductionDictionary[index];
if (!result) {
result = [self getDataAt:index];
self.dataProductionDictionary[index] = result;
}
return result;
}
然后 getDataAt:
是对您发布的代码的简单重构,除了不是循环 32k 元素,它只为传入的一个索引工作....
- (id)getDataAt:(NSNumber *)index {
int j = [index intValue];
// no loop, just skip to iteration j
NSArray *aItems = [_dictionaries[j] [@"prodA"] componentsSeparatedByString:@","];
NSArray *bItems = [_dictionaries[j] [@"prodB"] componentsSeparatedByString:@","];
// and so on, then at the end, don't save arrayItem, just return it
NSDictionary *arrayItem = @{@"name":_dictionaries[j] [@"name"],
@"type":[NSString stringWithFormat:@"%@",_dictionaries[j] [@"type"]],
@"height":[NSString stringWithFormat:@"%@",_dictionaries[j] [@"height"]],
@"aArray":productionAArray,
@"bArray":productionBArray,
};
return arrayItem;
}
PS - 可变字典是一种用于惰性求值的良好数据结构。下一个复杂级别是 NSCache,它就像一个可变字典,还管理内存 ( class ref here )。
关于ios - 使用 NSDictionary 元素缓慢创建 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27277899/
在 viewDidLoad 上,我的应用程序填充 NSMutableArray arrayOfHaiku,它是 NSDictionary 项的数组,如下所示: NSError *error; NS
我在 NSMutableArray 上是这样的: NSMutableArray *array = [NSMutableArray arrayWithObjects:@"0",@"0",@"0",@"0
我正在尝试设置我的 allArticlesArray 的内容,然后将附加对象附加到数组中。这是我的代码片段: [self.allArticlesArray setArray:newsArray]; [
我用 NSMutableArray 中的数据填充了 TableView ,一切正常。当我选择一个单元格(didSelectRowAtIndex)时,该项目将从数组中删除,正如它应该做的那样。现在我希望
我有一个 NSMutableArray 是我的代表,我也在我的一个 View Controller 中使用它。 所以在 viewDidLoad 我像这样制作我的 NSMutableArray 的可变副
我有 100 个元素的 NSMutableArray。 我想将“20 - 50”、“10 - 20”、“60 - 100”和 50 - 60. 元素加载到单独的 NSMutableArray 中,并将
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve t
我在尝试将字符串的 NSMutableArray 共享给另一个类时遇到了一些问题。我有一个 tableView,其中填充了 Strings,我想将其添加到 NSMutableArray。然后在另一个
正如我们所知,数组是一个连续的内存分配。那么 NSMutableArray 大小将如何增加。 最佳答案 C 数组确实使用连续内存,C++ std::vector 也是如此,但是 NSMutableAr
我需要对一个数组进行排序,里面有一个数组,像这样: NSMutableArray * array_example = [[NSMutableArray alloc] init]; [array_e
这看起来很简单,但我只是得到一个空数组。 我想获取一个按下的按钮,找到它旁边的所有按钮(参见代码中的注释),并将所有找到的按钮添加到另一个 NSMutableArray。 然后我想遍历该数组并对每个按
我正在尝试在另一个 NSMutableArray 中添加 NSMutableArray。但我想做的是嵌套数组。 我当前的代码是: NSMutableArray *array1 = [NSMutab
我有三个类,即DepartmentViewController、DepartmentRequest、Department。这就像我从 DepartmentViewcontroller 发出部门请求,并
我正在尝试将 NSMutableArray 存储在 NSMutableArray 中。所以 var all:NSMutableArray = NSMutableArray() let localArr
这一定是那些错误之一,您一直盯着代码看很长时间,以至于找不到错误。 我有这个代码块,我在其中循环遍历一个包含多个 NSMutableArray 的 NSMutableArray: // FoodVi
我有一个包含 50 个条目的 NSMutableArray - 有什么简单的方法可以将其分成 5 个 NSMutableArray,每个 10 个条目。 最佳答案 是的,要划分 NSMutableAr
我想将 NSMutableArray 的 addObject 放入 NSMutableArray 中,那么我如何快速实现这一点。并检索它,所以请帮助我,因为现在我正在快速学习。 间接说我要二维数组是指
好吧: 我有 NSMutableArray 1 我还有 NSMutableArray 2 我想从数组 1 中删除与数组 2 中的对象匹配的所有对象。 有什么想法吗? 最佳答案 我刚刚打开文档,打印 N
我正在尝试制作 NSMutableArray 的深拷贝,其对象是与此类似的自定义类的实例: @interface CustomParent : NSObject @property NSInteger
我知道你们可能会觉得这是一个重复的问题。但我对我的项目的这个问题搞砸了。 现在转到这个问题,我已经采取了NSMutableArray命名为 arr1包含字符串值,例如 @"34" , @"40"等等。
我是一名优秀的程序员,十分优秀!