使用嵌套循环加速算法

时间:2017-12-20 20:09:20

标签: optimization nested-loops

美好的一天!我请求专家帮助我优化程序。我的算法由七个嵌套循环组成,当然我无法将它们分开。搜索31 ^ 7次迭代只需48秒,不再需要!但是,如果在最后一级输入计算,则程序运行速度会慢得多。

int const N = 31;
int const M = 7;
int sym[6286];
int chast[501];
int mass[31];
register int c1;
register int c2;
register int c3;
register __int32 PHI_O;;
for (int k1 = 3; k1 < 6; k1++) {
    for (int k2 = 0; k2 < 31; k2++) {
        for (int k3 = 0; k3 < 31; k3++) {
            for (int k4 = 0; k4 < 31; k4++) {
                for (int k5 = 0; k5 < 31; k5++) {
                    for (int k6 = 0; k6 < 31; k6++) {
                        for (int k7 = 0; k7 < 31; k7++) {
                            memset(mass, 0, sizeof(mass));
                            memset(&PHI_O, 0, sizeof(PHI_O));
                            for (int i = 0; i < 501; i++) /*Must be optimized*/
                            {
                                c1 = k1 * sym[7 * i] + k2 * sym[7 * i + 1];
                                c2 = k3 * sym[7 * i + 2] + k4 * sym[7 * i + 3];
                                c3 = k5 * sym[7 * i + 4] + k6 * sym[7 * i + 5] + k7* sym[7 * i + 6];
                                chast[i] = (c1 + c2 + c3) % 31;
                                mass[chast[i]]++;
                            }
                            for (int i = 0; i < 31; i++) { /*Can I optimize?*/
                                PHI_O += mass[i] * (mass[i] - 1);
                            }
                            if (PHI_O > 10020) {
                                cout << PHI_O << '\n';
                                printf("%d %d %d %d %d %d %d\n", k1, k2, k3, k4, k5, k6, k7);
                                //cout << PHI_O << '\n';
                            }
                        }
                    }
                }
            }
        }
    }
}
void main(void) {
setlocale(LC_ALL, "RUS");
FILE * wr;
wr = fopen("C:\\FILE\\wr.txt", "r+");
char c;
int gl = 0;
do {
    c = fgetc(wr);
    if (c == ' ') continue;
    if (c == EOF) break;
    int k = c - '0';
    c = fgetc(wr);
    if (c == ' ') { sym[gl++] = k;  continue; }
    if (c == EOF) {
        sym[gl++]; break;
    }
    int l = c - '0';
    sym[gl++] = k * 10 + l;
} while (c != EOF);
thr1();
}

sym是一个数字数组,这里是整个程序! 提供所有优化方法直到汇编程序插入!我将非常感激!使用不是一种选择!

0 个答案:

没有答案