矩阵乘法小优化

看叉姐的repo翻到矩阵相关的板子

然后发现里面有这样一句话

乘法的时候将$B$数组转置一下然后$C[i][j] = \sum{A[i][k]\times B[j][k]}$会有奇效。

然后操作了一番发现的确是这么回事 Qrz%%%

1
2
3
4
5
6
7
8
9
10
11
12
13
14
srand(time(0));
f(i, 0, 233) f(j, 0, 233) a[i][j] = rand(), b[i][j] = rand();
double st = clock();
f(i, 0, 233) f(j, 0, 233) f(k, 0, 233)
c[i][j] += a[i][k] * b[k][j];
double ed = clock();
f(i, 0, 233) f(j, 0, 233) {
d[i][j] = b[j][i]; c[i][j] = 0;
}
f(i, 0, 233) f(j, 0, 233) f(k, 0, 233)
c[i][j] += a[i][k] * d[j][k];
double e2 = clock();
printf("%lf\n", ((e2 - ed) - (ed - st)) / CLOCKS_PER_SEC);
printf("%d\n", CLOCKS_PER_SEC);

本机大概能差0.02s

以及惊讶地发现CLOCKS_PER_SEC在新电脑上是1e6