【基础】查看程序运行所需时间--C++源代码(g++ 4.9.3)

来源:互联网 发布:淘宝店铺违规怎么申诉 编辑:程序博客网 时间:2024/05/17 19:20
#include <iostream>
#include <sys/time.h>

using namespace std;

int func()
{
        return 0;
}

int main()
{
        timeval start,end;

        gettimeofday(&start,NULL);
        for(int i=0;i<1000000;i++)
                func();
        gettimeofday(&end,NULL);

        double time = (end.tv_sec-start.tv_sec)*1000000 + (end.tv_usec-start.tv_usec);
    
        cout<<time<<endl;
        return 0;
}

原创粉丝点击