Eigen vs Numpy时间对比
有人说Eigen太慢了,用Numpy好一点,我们来看一看是不是这样的
Eigen C++ 程序 t_time.cpp
#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
void integer_time(){
int n_a_rows = 4000;
int n_a_cols = 3000;
int n_b_rows = n_a_cols;
int n_b_cols = 200;
MatrixXi a(n_a_rows, n_a_cols);
for(int i =0; i<n_a_rows; ++i){
for(int j =0; j<n_a_cols; ++j){
a(i,j) = n_a_cols *i+j;
}
}
MatrixXi b(n_b_rows, n_b_cols);
for(int i =0; i<n_b_rows; i++){
for(int j=0; j<n_b_cols; j++){
b(i, j) = n_b_cols * i+j;
}
}
MatrixXi d(n_a_rows, n_b_cols);
clock_t begin = clock();
d = a*b;
clock_t end = clock();
double sec = double(end- begin) /CLOCKS_PER_SEC;
std::cout<<sec<<std::endl;
std::cout << "integer time" << std::endl;
}
void double_time(){
int n_a_rows = 4000;
int n_a_cols = 3000;
int n_b_rows = n_a_cols;
int n_b_cols = 200;
MatrixXd a(n_a_rows, n_a_cols);
for(int i =0; i<n_a_rows; ++i){
for(int j =0; j<n_a_cols; ++j){
a(i,j) =(double) (n_a_cols *i+j);
}
}
MatrixXd b(n_b_rows, n_b_cols);
for(int i =0; i<n_b_rows; i++){
for(int j=0; j<n_b_cols; j++){
b(i, j) =(double)( n_b_cols * i+j);
}
}
MatrixXd d(n_a_rows, n_b_cols);
clock_t begin = clock();
d = a*b;
clock_t end = clock();
double sec = double(end- begin) /CLOCKS_PER_SEC;
std::cout<<sec<<std::endl;
std::cout << "double time" << std::endl;
}
int main()
{
integer_time();
double_time();
return 0;
}
使用cmake 来编译
cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O2 -DNDEBUG")
find_package(Eigen3 REQUIRED)
include_directories(
${PROJECT_SOURCE_DIR}
${EIGEN3_INCLUDE_DIRS}
)
add_executable(a t_time.cpp)
target_link_libraries(a ${Eigen3_LIBS})
或者直接编译:g++ -std=c++11 -I/usr/include/eigen3 -O2 -DNDEBUG t_time.cpp -o a
输出结果:
1.15959
integer time
0.60962
double time
Numpy 程序 t_time.py
#!/usr/bin/env python
# coding=utf-8
import numpy as np
import time
n_a_rows = 4000
n_a_cols = 3000
n_b_rows = n_a_cols
n_b_cols = 200
a = np.arange(n_a_rows * n_a_cols).reshape(n_a_rows, n_a_cols)
b = np.arange(n_b_rows * n_b_cols).reshape(n_b_rows, n_b_cols)
print(a.dtype)
start = time.time()
d = np.dot(a, b)
end = time.time()
print "integer time : {}".format(end - start)
a = np.arange(n_a_rows * n_a_cols).reshape(n_a_rows, n_a_cols)*1.0
b = np.arange(n_b_rows * n_b_cols).reshape(n_b_rows, n_b_cols)*1.0
print(a.dtype)
start = time.time()
d = np.dot(a, b)
end = time.time()
print "double time : {}".format(end - start)
输出结果:
int64
integer time : 2.97904801369
float64
double time : 0.0668978691101
总结
- int 型的时候c++比较快, double型的时候numpy比较快
- 但两者都差不多
- numpy用起来比较舒服,代码量非常少
- C++ 使用了
-O2 -DNDEBUG
做编译优化, 不然时间都需要10几秒
C++ 没有使用编译优化时:
16.0714
integer time
18.6559
double time