Eigen vs Numpy時間對比


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM