私はたくさんのオブジェクトを持っています、それぞれがNSDictionaryに保存されているいくつかのタグを持っています。また、ループがあり、すべてのオブジェクトを反復処理し、オブジェクトのタグに応じていくつかの処理を実行します。メソッドobjectForKey:呼び出しが多すぎると、パフォーマンスが大幅に低下します。どうすればパフォーマンスを向上させることができますか?たとえば、@ propertyに「copy」ではなく「strong」を設定してみましたが、問題は解決しませんでした。ありがとう!
編集:
これはある種のコードです。もちろん、私のプロジェクトのコードよりもはるかに単純です。プロファイラーによると、ホットスポットはdoSomeStuff:ではなくobjectForKey:にあります。それはその呼び出しの量のためである可能性がありますか?タグにはオブジェクトがほとんどなく、オブジェクトごとに約10個ありますが、オブジェクトの数はかなり多くなります
.h:
@interface MyObject : NSObject
@property (nonatomic, readwrite, strong/*copy*/ ) NSDictionary *tags;
-(void)doSomeStuff;
@end
.m:
@implementation MyObject
{
NSDictionary *tags; // about 10 values for each object
}
-(NSDictionary *)tags
{
return tags;
}
-(void)setTags:(NSDictionary *)newTags
{
tags = [newTags copy];
}
-(void)doSomeStuff;
@end
使用:
NSArray *objects = ... // a lot of items, > 1000
NSArray *rules = ... // NSArray of objects with property NSArray* Strings, about 50 strings
for (MyObject *object in objects)
{
for (NSArray *rule in rules)
{
for (NSString *ruleString in rule.Strings)
{
// there is a more complicated check in my project, so you may imagine that there are 10 calls of objectForKey:, with different keys
if ([object objectForKey:ruleString] isEqualToString:@"yes"])
{
[object doSomeStuff];
}
}
}
}