我有一个 PhoneGap 移动应用程序,我需要生成一组匹配组合。在 JavaScript 方面,当生成组合的数组变大时,代码很快就会挂起。所以,我想我会制作一个插件来生成组合,将 javascript 对象数组传递到本机端并在那里循环。
令我惊讶的是,以下代码在 150 毫秒 (JavaScript) 内执行,而在本机端 (Objective-C) 则需要约 1000 毫秒。
有谁知道加快这些执行时间的任何技巧?当玩家超过 10 人时,即团队数组的长度等于 252 时,它真的变慢了。上面提到的执行时间是针对 10 名玩家 / 252 支球队的。
这是 JavaScript 代码:
for (i = 0; i < GAME.teams.length; i += 1) {
for (j = i + 1; j < GAME.teams.length; j += 1) {
t1 = GAME.teams[i];
t2 = GAME.teams[j];
if ((t1.mask & t2.mask) === 0) {
GAME.matches.push({
Team1: t1,
Team2: t2
});
}
}
}
...这是本机代码:
NSArray *teams = [[NSArray alloc] initWithArray: [options objectForKey:@"teams"]];
NSMutableArray *t = [[NSMutableArray alloc] init];
int mask_t1;
int mask_t2;
for (NSInteger i = 0; i < [teams count]; i++) {
for (NSInteger j = i + 1; j < [teams count]; j++) {
mask_t1 = [[[teams objectAtIndex:i] objectForKey:@"mask"] intValue];
mask_t2 = [[[teams objectAtIndex:j] objectForKey:@"mask"] intValue];
if ((mask_t1 & mask_t2) == 0) {
[t insertObject:[teams objectAtIndex:i] atIndex:0];
[t insertObject:[teams objectAtIndex:j] atIndex:1];
/*
NSArray *newCombination = [[NSArray alloc] initWithObjects:
[teams objectAtIndex:i],
[teams objectAtIndex:j],
nil];
*/
[combinations addObject:t];
}
}
}
...有问题的数组(GAME.teams)如下所示:
{
count = 2;
full = 1;
list = (
{
index = 0;
mask = 1;
name = A;
score = 0;
},
{
index = 1;
mask = 2;
name = B;
score = 0;
}
);
mask = 3;
name = A;
},
{
count = 2;
full = 1;
list = (
{
index = 0;
mask = 1;
name = A;
score = 0;
},
{
index = 2;
mask = 4;
name = C;
score = 0;
}
);
mask = 5;
name = A;
},