我正在尝试使用 C++ 和 numpy 比较特征值的矩阵乘法性能。
这是矩阵乘法的c++代码
#include<iostream>
#include <Eigen/Dense>
#include <ctime>
#include <iomanip>
using namespace Eigen;
using namespace std;
int main()
{
time_t begin,end;
double difference=0;
time (&begin);
for(int i=0;i<500;++i)
{
MatrixXd m1 = MatrixXd::Random(500,500);
MatrixXd m2 = MatrixXd::Random(500,500);
MatrixXd m3 = MatrixXd::Zero(500,500);
m3=m1*m2;
}
time (&end);
difference = difftime (end,begin);
std::cout<<"time = "<<std::setprecision(10)<<(difference/500.)<<" seconds"<<std::endl;
return 0;
}
使用 g++ -Wall -Wextra -I "path-to-eigen-directory"prog5.cpp -o prog5 -O3 -std=gnu++0x
编译
输出:
时间 = 0.116 秒
这是python代码。
import timeit
import numpy as np
start_time = timeit.default_timer()
for i in range(500):
m1=np.random.rand(500,500)
m2=np.random.rand(500,500)
m3=np.zeros((500,500))
m3=np.dot(m1,m2)
stop_time = timeit.default_timer()
print('Time = {} seconds'.format((stop_time-start_time)/500))
输出:
时间 = 0.01877937281645333 秒
看起来 C++ 代码比 Python 慢 6 倍。有人可以提供我是否在这里遗漏任何东西的见解吗?
我正在使用 Eigen 3.3.4、g++ 编译器 (MinGW.org GCC-6.3.0-1) 6.3.0、python 3.6.1、numpy 1.11.3。使用 spyder ide 运行的 Python。使用 Windows。
更新:
根据回答和评论,我更新了代码。
使用 g++ -Wall -Wextra -I "path-to-eigen-directory"prog5.cpp -o prog5 -O3 -std=gnu++0x -march=native
编译的 C++ 代码。我无法让 -fopenmp
工作 - 如果我使用这个标志,似乎没有输出。
#include<iostream>
#include <Eigen/Dense>
#include <ctime>
#include <iomanip>
using namespace Eigen;
using namespace std;
int main()
{
time_t begin,end;
double difference=0;
time (&begin);
for(int i=0;i<10000;++i)
{
MatrixXd m1 = MatrixXd::Random(500,500);
MatrixXd m2 = MatrixXd::Random(500,500);
MatrixXd m3 = MatrixXd::Zero(500,500);
m3=m1*m2;
}
time (&end); // note time after execution
difference = difftime (end,begin);
std::cout<<"Total time = "<<difference<<" seconds"<<std::endl;
std::cout<<"Average time = "<<std::setprecision(10)<<(difference/10000.)<<" seconds"<<std::endl;
return 0;
}
输出:
Total time = 328 seconds
Average time = 0.0328 seconds
Python代码:
import timeit
import numpy as np
start_time = timeit.default_timer()
for i in range(10000):
m1=np.random.rand(500,500)
m2=np.random.rand(500,500)
m3=np.zeros((500,500))
m3=np.dot(m1,m2)
stop_time = timeit.default_timer()
print('Total time = {} seconds'.format(stop_time-start_time))
print('Average time = {} seconds'.format((stop_time-start_time)/10000))
使用 spyder IDE 使用 runfile('filename.py')
命令运行。
输出:
Total time = 169.35587796526667 seconds
Average time = 0.016935587796526666 seconds
现在 eigen 的性能更好,但不等于或快于 numpy。可能是 -fopenmp
可以解决问题,但不确定。但是,我没有在 numpy 中使用任何并行化,除非它隐式地这样做。
最佳答案
您的基准测试有几个问题:
- 您正在对系统
rand()
函数进行基准测试,这非常昂贵! - 您缺少编译器
-march=native
以获得 AVX/FMA 提升 - 您缺少启用多线程的
-fopenmp
。
在我的 quad i7 2.6GHz CPU 上,我得到:
initial code: 0.024s
after replacing `Random` by `Ones`: 0.018s
adding `-march=native`: 0.006s
adding `-fopenmp`: 0.003s
矩阵有点太小,无法获得良好的多线程优势。
关于c++ - 矩阵乘法性能 numpy 和 eigen c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47878961/