如何在iOS

时间:2016-03-25 08:58:39

标签: ios performance

我想知道是否有任何技术可以在iOS上更快地循环,就像在下面这段代码中一样

while (count < 1000000000) {

    for (int j = 0; j < 16; j++) {

        target = [[ballTargetArray objectAtIndex:j]integerValue];
        destination = [[ballDestinationArray objectAtIndex:j]integerValue];

        if (target != destination) {

            [ballPosition exchangeObjectAtIndex:(destination -1) withObjectAtIndex:(target-1)];
        }else{

            NSLog(@"same target and destination");
            return;
        }
    }
    count++;

    if (count %1000000 == 0) {
        printf("Round>>%d\n", count);// tracking
    }
}

现在每百万计算大约需要4-6秒。 我只是不知道从哪里开始寻找。

由于

1 个答案:

答案 0 :(得分:0)

因为没有人给出可接受的答案,所以我将回答我迄今为止发现的问题。 如果有人有更好的答案,请不要犹豫。

由于@Wain和@NSDmitry建议在通过索引

访问其值时使用简单的C数组

我也尝试通过for for for(只是围绕for循环)进行优化 1000 * 1000 * 1000 = 1000000000

当我这样做时,我可以将时间从小时缩短到不到5分钟

    for (int k = 0; k < 1000; k++) {
        for (int i = 0; i < 1000; i++) {
            for (int j = 0; j < 1000; j++) {
                for (int j = 0; j < numberOfOperation; j++) {
                    [ballPoaition exchangeObjectAtIndex:(destinationIntArr[j]-1) withObjectAtIndex:(targetIntArr[j]-1)];
                }
            }
        }
        printf("Round>>%d\n", k * 1000000);
    }

如果大数在(10 ^ 1 - 10 ^ 9)之间是随机的,我仍然不知道该怎么做

但无论如何,感谢所有的建议

相关问题