您的位置:首页 > 编程语言 > C语言/C++

c++如何计算程序运行的时间

2015-09-28 16:08 706 查看
方案一:

#include<iostream>
#include<ctime>
using namespace std;
 
int main()
{
    clock_t start,finish;
    start=clock();
     
    cout << "HW .... " << endl;
 
    finish=clock();
 
    cout << finish-start   << "/" << CLOCKS_PER_SEC  << " (s) "<< endl;
     
    return 0;
}

方案二:

clock_t st = clock();

.......

......

printf("time: %.0lfms\n", (clock()-st)*1000.0 / CLOCKS_PER_SEC);


方案三:

c语言计算函数消耗时间:

#include <sys/time.h>

#include <unistd.h>

struct timeval tv1, tv2;

double sec =0;

gettimeofday(&tv1, 0);

HEGGTOPCOLLECTOR hTopCollector = eggTopCollector_new(0); // top
10

gettimeofday(&tv2, 0);

sec = (double)(tv2.tv_sec - tv1.tv_sec) + (double)(tv2.tv_usec - tv1.tv_usec) /1000000;

printf("time1: %f\n",
sec);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: