您的位置:首页 > 运维架构 > Linux

Linux下测试程序的运行时间

2013-12-19 20:52 120 查看
方法一:

要包含头文件

#include "time.h"

#include <sstream>

代码如下:

[cpp] view
plaincopy

/*...............测试程序运行时间...................*/

time_t startT,endT;

double totalT;

startT = time(NULL);

// 假设一条语句执行10000次

// obj.MysqlInsert("insert into children values(22,'zhangsan',50)"); //插入一行数据

for(int i=0; i<10000; ++i)

{

stringstream ss;

ss << "insert into children values("<< i << ",'cershi',10)";

obj.mysql_Insert(ss.str()); //插入一行数据

} //这里插入了1万行数据

endT = time(NULL);

totalT = difftime(endT,startT);

cout << "程序执行的时间为:" << totalT << endl;

方法二:

#include <stdio.h>

#include <sys/time.h>

#include <unistd.h>

int main()

{

struct timeval tpstart,tpend;

float timeuse;

gettimeofday(&tpstart,NULL);

for(int i=0;i<10;i++)

{

usleep(200000);//暂停200ms

}

gettimeofday(&tpend,NULL);

timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;

timeuse/=1000000;

printf("Used Time:%f\n",timeuse);

return 0;

}

方法三:

在执行程序前,加time,如:输入time./abc
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: