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;
}
);