基本上,我的 iPhone 应用程序中有一个图形视图控制器(实际上是几个)。这些图表取决于用户对不同“记分卡”的选择。然而,问题是用户可以选择任意数量的记分卡:他们可以选择数百个。
在生成图表时,我运行了许多算法,它们非常繁重,因为它们会深入核心数据以提取信息,下面是我的一种算法的示例。
-(NSDictionary *) stablefordForEachPersonOnCourse:(NSString *) courseName atDate:(NSString *) date
{
NSDictionary *shotsForEachPersonOnCourse = [[NSDictionary alloc]initWithDictionary:[self shotsForEachPersonOnCourse:courseName atDate:date]];
CoreDataHelper *helper = [[CoreDataHelper alloc]init];
NSMutableDictionary *stablefordWithNames = [[NSMutableDictionary alloc]init];
ScoreCard *card = [helper getScoreCardWithDate:date fromCourse:[helper getGolfCourseWithName: courseName]];
for (int j = 0 ; j < [[shotsForEachPersonOnCourse allKeys]count]; ++j) {
Player *player = [helper getPlayerWithName:[[shotsForEachPersonOnCourse allKeys]objectAtIndex:j] fromScoreCard:card];
NSString *teeColour = player.teePlayed;
NSArray *arrayOfHolesPlayed = [helper getOrderedArrayOfHolesFromPlayer:player];
NSArray *arrayOfHoles = [helper getOrderedArrayOfHolesFromTee:[helper getTeeWithColour:teeColour fromCourse:[helper getGolfCourseWithName:courseName]]];
NSMutableArray *stableFord = [[NSMutableArray alloc]init];
for (int i = 0; i< [arrayOfHolesPlayed count]; i++) {
HolePlayed *holePlayed = [arrayOfHolesPlayed objectAtIndex:i];
Hole *hole = [arrayOfHoles objectAtIndex:i];
int temp1 = 0, shotsGet = 0;
int handicap = player.handicap.intValue;
int strokeIndex = hole.holeSI.intValue;
int par = hole.holePar.intValue;
int shotScore = holePlayed.holeScore.intValue;
if (shotScore >0) {
while (temp1 >= 0) {
if(handicap - (strokeIndex + (18*temp1))>= 0)
{
++shotsGet;
++temp1;
}
else temp1 = -1;
}
int stableford = (0 - (shotScore - (shotsGet + par))+2);
if (stableford < 0) {
stableford = 0;
}
[stableFord addObject:[NSNumber numberWithInt:stableford]];
}
else if (shotScore <1) [stableFord addObject:[NSNumber numberWithInt:0]];
}
[stablefordWithNames setValue:stableFord forKey:[[shotsForEachPersonOnCourse allKeys]objectAtIndex:j]];
}
return stablefordWithNames;}
该算法计算特定记分卡上每个人的 stableford。现在,当用户选择许多记分卡时,我需要一种快速提高此类算法速度的方法。
我已经把这个问题留得很开放了——但是加速我的代码的最好方法是什么。显然,必须使用 Grand Central Dispatch (GCD),因为这使代码能够在不同的内核上运行,从而提高每单位时间的处理能力 - 但是在此使用 GCD 的最佳方法是什么?另外,我应该把我所有的计算变成块吗?这会显着提高计算速度吗?