【问题标题】:Code runs faster with Rcpp than compiled with g++使用 Rcpp 的代码运行速度比使用 g++ 编译的快
【发布时间】:2018-03-28 21:22:49
【问题描述】:

我以两种方式运行完全相同的代码:使用g++ 编译器编译它,以及使用RcppR 调用它。事实证明,当我通过R 运行它时,它的运行速度几乎快了 4 倍。

为什么会这样?是不是因为Rcpp使用的编译器不一样?

这是我在c++中运行的代码:

#include <iostream>
#include <nlopt.hpp>
#include <time.h>
using namespace std;


int main()
{

  //--------------------------------//
  //         Initialization         //
  //--------------------------------//

  // Grid for x
  const int nx              = 60000;

  float xgrid[nx];
  const float xstep = 4 /(nx - 1);
  float it = 0;

  for(int i = 0; i < nx; i++){
    xgrid[i] = it*xstep;
    it++;
  }

  // Initialize value function V
  size_t sizeV     = nx*sizeof(float);
  float *V;
  V     = (float *)malloc(sizeV);


  //--------------------------------//
  //         Computation            //
  //--------------------------------//

  // Variables for computation time
  double t0  = clock();
  double t   = t0;

  float utility;
  float VV = pow(-10.0,5.0);

  for(int ix = 0; ix<nx; ix++){

    VV = pow(-10.0,5.0);

    for(int ixp = 0; ixp < nx; ixp++){

      utility = (xgrid[ix] + 1 - xgrid[ixp])*(xgrid[ix] + 1 - xgrid[ixp]);

      if(utility >= VV){
        VV = utility;
      }
    }

    V[ix] = VV;
  }

  t = clock() - t0;
  cout << "Time: " << ((float)t)/CLOCKS_PER_SEC << " seconds." << endl;

  return 0;
}

要运行它,我使用:

g++ Cpp_main.cpp -o Cpp_main

Rcpp中的代码是:

#include <iostream>
#include <nlopt.hpp>
#include <time.h>
using namespace std;


// [[Rcpp::export]]
vector<double> value(int nx){ 

  //--------------------------------//
  //         Grid creation          //
  //--------------------------------//

  float xgrid[nx];

  const float xstep = 4 /(nx - 1);
  float it = 0;

  for(int i = 0; i < nx; i++){
    xgrid[i] = it*xstep;
    it++;
  }

  // Initialize value function V
  vector<double> V;
  V.resize(nx);


  //--------------------------------//
  //           Computation          //
  //--------------------------------//

  // Variables for computation time
  double t0  = clock();
  double t   = t0;

  float utility;
  float VV = pow(-10.0,5.0);

  for(int ix = 0; ix<nx; ix++){

    VV = pow(-10.0,5.0);

    for(int ixp = 0; ixp < nx; ixp++){

      utility = (xgrid[ix] + 1 - xgrid[ixp])*(xgrid[ix] + 1 - xgrid[ixp]);

      if(utility >= VV){
        VV = utility;
      }
    }

    V[ix] = VV;
  }

  t = clock() - t0;
  cout << "Time: " << ((float)t)/CLOCKS_PER_SEC << " seconds." << endl;

  return V;
}

我从R 调用它:

library("Rcpp")
sourceCpp("Rcpp_main.cpp")

# Grid for x
nx            = 60000; 

V = value(nx);

c++ 中的运行时间是Rcpp 中运行时间的两倍。任何线索为什么会发生这种情况?

【问题讨论】:

标签: r performance g++ rcpp


【解决方案1】:

只要看看你的main(),我们就知道了:

edd@rob:/tmp/soQ$ g++ -o main main.cpp 
edd@rob:/tmp/soQ$ ./main 
Time: 8.42708 seconds.
edd@rob:/tmp/soQ$ g++ -o main -O3 -march=native main.cpp 
edd@rob:/tmp/soQ$ ./main 
Time: 1.59151 seconds.
edd@rob:/tmp/soQ$ 

这已经是 5.3 的一个因素,并且是我在一段时间内看到的关于 -O3 影响的最奇怪的例子之一。

对于 R,我得到的时间与 R 默认在这里也使用 -O3 的时间差不多

R> Rcpp::sourceCpp("/tmp/soQ/rcppfunction.cpp")    
R> V <- value(60000)
Time: 1.65224 seconds.    
R>  

所以这里没有真正的谜团。您使用了不同的选项,这很重要。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    相关资源
    最近更新 更多