honghao0 2020-07-27
这是我用所有三种语言运行矩阵乘法时发生的情况

在过去的两年中,我为C语言做了大量的实现工作。 我之所以选择C语言而不是其他语言,是因为人们普遍认为C代码比其他流行的编程语言(例如Java和Python)运行得更快。 但是,即使我一直对C的速度(或C实际上最快)感到好奇,我自己也没有做任何实验来证实这一说法。 最后,我决定进行一些实验,以比较C,Java和Python的性能。 本文是关于我进行的实验和获得的结果的文章。
本实验
我决定使用所有三种语言进行矩阵乘法。 矩阵的大小为2048 x 2048(即每个矩阵的乘法和加法运算为8,589,934,592),我为它们填充了0.0到1.0之间的随机值(使用随机值而不是对所有三种语言使用完全相同的矩阵的影响可以忽略不计)。 我将每个实验运行了五次,并计算了平均运行时间。
C代码
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define n 2048
double A[n][n];
double B[n][n];
double C[n][n];
int main() {
//populate the matrices with random values between 0.0 and 1.0
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = (double) rand() / (double) RAND_MAX;
B[i][j] = (double) rand() / (double) RAND_MAX;
C[i][j] = 0;
}
}
struct timespec start, end;
double time_spent;
//matrix multiplication
clock_gettime(CLOCK_REALTIME, &start);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
clock_gettime(CLOCK_REALTIME, &end);
time_spent = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.0;
printf("Elapsed time in seconds: %f \n", time_spent);
return 0;
} Java代码
import java.util.Random;
public class MatrixMultiplication {
static int n = 2048;
static double[][] A = new double[n][n];
static double[][] B = new double[n][n];
static double[][] C = new double[n][n];
public static void main(String[] args) {
//populate the matrices with random values between 0.0 and 1.0
Random r = new Random();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = r.nextDouble();
B[i][j] = r.nextDouble();
C[i][j] = 0;
}
}
long start = System.nanoTime();
//matrix multiplication
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
long stop = System.nanoTime();
double timeDiff = (stop - start) * 1e-9;
System.out.println("Elapsed time in seconds: " + timeDiff);
}
} Python代码
import random
import time
n = 2048
#populate the matrices with random values between 0.0 and 1.0
A = [[random.random() for row in range(n)] for col in range(n)]
B = [[random.random() for row in range(n)] for col in range(n)]
C = [[0 for row in range(n)] for col in range(n)]
start = time.time()
#matrix multiplication
for i in range(n):
for j in range(n):
for k in range(n):
C[i][j] += A[i][k] * B[k][j]
end = time.time()
print("Elapsed time in seconds %0.6f" % (end-start))
如何编译和运行
#C
gcc MatrixMultiplication.c -o matrix
./matrix
#Java
javac MatrixMultiplication.java
java MatrixMultiplication
#Python
python MatrixMultiplication.py 如何编译和运行
#C gcc MatrixMultiplication.c -o matrix ./matrix #Java javac MatrixMultiplication.java java MatrixMultiplication #Python python MatrixMultiplication.py
运行时间

根据这些结果,C比Java慢2.34倍,Python比Java慢33.34倍。
等待!!! C应该不是最快的吗???
实际上,这是不公平的比较。 当我们编译Java程序时,即使没有任何优化标志,Java JIT(即时)编译器也会自动执行优化。 但是,对于GCC(编译C程序),情况并非如此,我们必须显式设置优化标志。
因此,我在编译C程序时使用了-O2和-O3优化标志,并再次进行了实验。
gcc -O2 MatrixMultiplication.c -o matrix./matrixgcc -O3 MatrixMultiplication.c -o matrix./matrix
新的经过时间

现在,Java代码比C [-O3]慢1.69倍,而Python代码慢56倍。 我做出了正确的决定(或者很幸运:-)),选择了C而不是其他编程语言。
总结结果
