c++ time

        static uint64_t getCurrentSystemTime()
        {
            timeval tv;
            gettimeofday(&tv, 0);
            uint64_t time = (uint64_t)(tv.tv_sec * 1000000 + tv.tv_usec);
            return time;
        }

    timeval start;
    gettimeofday(&start, 0);
    uint64_t beginning = start.tv_sec * 1000000 + start.tv_usec;
    uint64_t duration = getCurrentSystemTime() - start;
    cout<<"Stopwatch measured mean rum time with read image from dist:"                           <<(double)duration/1000000.0<<" S"<<endl;

#include<ctime>
int main(int argc,char** argv)
{
  clock_t time_tt = clock();//计时开始
  
  cout<<"time use is"<<1000*(clock()-time_tt)/(double)CLOCK_PER_SEC<<"ms"<<endl;
	//cout <<"time use in normal invers is " << 1000* (clock() - time_stt)/(double)CLOCKS_PER_SEC << "ms"<< endl;
}
// 计算函数执行多长时间的代码复用

template<class T>
void measure(T&& func)
{
    using naemspace std::chrono;
    suto start = system_clock::now();
    func();
    duration<double> diff = system_clock::now()-start;
    cout<<"elapsed:"<<diff.count()<<"seconds"<<endl;
}
void func()
{
    // TO DO
}
// 求和函数
const long s = 1000000000;
void sum(long strat,long end,long& ans)
{
    long s = 0;
    for(long i = strat; i < end; i++){
        s+=i;
    }
    ans = s;

}
// 函数指针调用
measure(func);
// lamada 表达式调用
// 普通加法
measure([]()
{
    long ans;
    sum (0,S,ans);
    cout<<ans<<endl;
}
);
//多线程加速
measure([]()
{
    long ans1,ans2;
    thread thread1 = thread(sum,0,S/2,std::ref(ans1));
    thread thread2 = thread(sum,S/2,0,std::ref(ans2));
    thread1.join();
    thread2.join();
    cout<<(ans1+ans2)<<endl;
}
);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值