犰狳图书馆的矢量有什么问题

时间:2015-02-11 06:37:55

标签: c++ performance iteration armadillo

之前我发布的question在某种程度上有点复杂。在这个问题中,我做了一个简单的例子,用Armadillo库生成同样的问题,这应该是非常快的。

考虑以下具有两个主要功能的代码,每次只激活一个:

#include <armadillo>

const int runmax=50000000;
arma::vec::fixed<3u> x,y;
double a,b;

void init()
{
    x<<0.2<<arma::endr<<-0.3<<arma::endr<<0.1;
    y<<1<<arma::endr<<1<<arma::endr<<1;
    a=0.3;
    b=a*a;
}

template<unsigned N>
void scaledsum(double a,arma::vec::fixed<N> &x,double b,arma::vec::fixed<N> &y)
{
    for(int i=0;i<N;i++)
    {
        y(i)=a*x(i)+b*y(i);
    }
}

void main1()
{

    for(int i=0;i<runmax;i++)
    {
        y=a*x+b*y;
    }
}

void main2()
{
    for(int i=0;i<runmax;i++)
    {
        scaledsum(a,x,b,y);
    }
}

int main()
{
    init();

    //main1();
    main2();
    y.print();
    return 0;
}
  

perf stat ./main1

     

perf stat ./main2

我希望main1的运行速度比main2快。或者至少非常接近它。但是main1运行速度较慢。

我不明白这样的分析:

MAIN1

  

0.209682235秒时间

MAIN2

  

0.121644777秒时间

PS 编译命令:g++ -std=c++11 -O3 -s -DNDEBUG test.cpp

0 个答案:

没有答案