Eigen vs Numpy时间对比
有人说Eigen太慢了,用Numpy好一点,我们来看一看是不是这样的
Eigen C++ 程序 t_time.cpp
#include#include 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
使用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.15959integer time0.60962double time
Numpy 程序 t_time.py
#!/usr/bin/env python# coding=utf-8import numpy as npimport timen_a_rows = 4000n_a_cols = 3000n_b_rows = n_a_colsn_b_cols = 200a = 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.0b = np.arange(n_b_rows * n_b_cols).reshape(n_b_rows, n_b_cols)*1.0print(a.dtype)start = time.time()d = np.dot(a, b)end = time.time()print "double time : {}".format(end - start)
输出结果:
int64integer time : 2.97904801369float64double time : 0.0668978691101
总结
- int 型的时候c++比较快, double型的时候numpy比较快
- 但两者都差不多
- numpy用起来比较舒服,代码量非常少
- C++ 使用了
-O2 -DNDEBUG
做编译优化, 不然时间都需要10几秒
C++ 没有使用编译优化时:
16.0714integer time18.6559double time