NSMutableDictionary 对每个值应用公式的最有效方法

发布于 2024-12-25 20:09:49 字数 439 浏览 0 评论 0原文

我有一个带有 NSNumbers 的 NSMutableDictionary。当我完成构建集时,我需要使用当前存储的值本身重新计算所有值。现在我正在使用快速枚举并存储到一个新的 NSMutableSet 中,但我在 Objective C 方面没有经验,必须有一种更有效的方法来做到这一点:

for (id key in temp_target_results) 
{
        formula_score = MyFormula([[temp_target_results objectForKey:key] doubleValue]);

        [target_results setObject:[NSNumber numberWithDouble:formula_score] forKey:key];
}

最后我按值排序(这就是为什么我使用 NSMutableSet)。

I have a NSMutableDictionary with NSNumbers. When I finish building the set I need to recalculate all the values using the currently stored value itself. Now I'm using fast enumeration and storing into a new NSMutableSet, but I'm not experienced in Objective C and there must be a more efficient way to do this:

for (id key in temp_target_results) 
{
        formula_score = MyFormula([[temp_target_results objectForKey:key] doubleValue]);

        [target_results setObject:[NSNumber numberWithDouble:formula_score] forKey:key];
}

In the end I'm sorting by value (that's why I'm using NSMutableSet).

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

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

发布评论

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

评论(1

情释 2025-01-01 20:09:49

我不知道还有多少人可以帮助您优化正在做的事情,因为我不知道幕后发生了什么(在您上面粘贴的代码片段的上下文之外,或者您的内部到底发生了什么) MyFormula 函数)。

我的一个优化问题是:为什么你将所有内容存储为 NSNumber 对象而不是双精度数组?这样做的唯一优势(我目前可以看到)是,如果您将 NSNumber 对象以 NSArrayNSSetNSDictionary 被写入磁盘或在 NSNotification 中传递。

但我要做的一些事情包括摆脱对 objectForKey 的额外、不必要的调用:

for (NSNumber * myNumber in temp_target_results) 
{
   formula_score = MyFormula([myNumber doubleValue]);

   [target_results setObject:[NSNumber numberWithDouble:formula_score] forKey:[myNumber stringValue]];
}

这里的另一个考虑因素:

您的 target_results 似乎不是 NSMutableSet 因为您正在执行 setObject:forKey:NSMutableDictionary 方法。如果 target_results 确实是一个 NSMutableSet,您只需要调用:

[target_results addObject: [NSNumber numberWithDouble: formula_score]];

I don't know how much more anyone can help you optimize what you are doing because I don't know what's going on behind the scenes (outside of the context of the snippet of code you pasted above, or what's really going on inside your MyFormula function).

One optimization question I have would be: why are you storing everything as NSNumber objects anyways and not an array of doubles? The only advantage (that I can currently see) to doing that is if you're passing your NSNumber objects along in an NSArray, NSSet or NSDictionary that gets written out to disk or passed along in an NSNotification or.

But some of the things I would do would include getting rid of that extra, unnecessary call to objectForKey:

for (NSNumber * myNumber in temp_target_results) 
{
   formula_score = MyFormula([myNumber doubleValue]);

   [target_results setObject:[NSNumber numberWithDouble:formula_score] forKey:[myNumber stringValue]];
}

Another consideration here:

Your target_results appears to not be a NSMutableSet because you are doing a NSMutableDictionary method of setObject:forKey:. If target_results really was a NSMutableSet, you'd only need to call:

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