yang131

导航

关于右值引用测试

不论在win: vs,gcc 测试,使用RightValue,性能出现下降

 

 

在Llvm下,使用RightValue,性能也出现下降:

 

测试参考之前的博客代码,现有的代码也可以:

#include <iostream>
#include <vector>
#include <utility>
#include <chrono>
using namespace std;
using namespace std::chrono;

struct A{
int a = 1000;
int b;
char c;
short s;
char cc[22];
};

void Recurve(int n,A a){
    //cout<<n<<endl;
    a.a--;
    if(n > 0)
        Recurve(n-1,a);
}
void RecurveRe(int n,A& a){
    //cout<<n<<endl;
    a.a--;
    if(n > 0)
        RecurveRe(n-1,a);
}
void RecurveRV(int n,A&& a){
    //cout<<n<<endl;
    a.a--;
    if(n > 0)
        RecurveRV(n-1,forward<A>(a));
}

class Timer
{
public:
    void Start()
    {
        start = steady_clock::now();
    }
    void Stop()
    {
        std::chrono::steady_clock::time_point end = steady_clock::now();

        steady_clock::duration time_span = end - start;
        cout << "total time" << duration_cast<nanoseconds>(time_span).count() << endl;
    }
    std::chrono::steady_clock::time_point start;

};

int test()
{
    A a;
    Timer t;

    t.Start();

    Recurve(10000,a);
    //RecurveRe(1000,a);
   // RecurveRV(1000,move(a));
    cout<<"endl"<<endl;
    //Enum(v, n, count);
    t.Stop();
    return 0;
}

  

posted on 2024-11-11 20:14  NoNight  阅读(0)  评论(0编辑  收藏  举报