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
要包含头文件
#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
相关文章推荐
- Linux下测试程序运行时间
- Linux下测试程序的运行时间
- Linux下测试程序运行时间方法
- Linux下测试程序运行时间
- Linux下测试程序运行时间模板
- linux下测试程序运行时间
- linux下测试程序运行的时间
- 【Linux】【C语言】菜鸟学习日志(一) 一步一步学习在Linxu下测试程序的运行时间
- linux下测试程序运行的时间
- C#中提供的精准测试程序运行时间的类Stopwatch
- 测试程序运行时间
- 如何测试程序在DSP上的运行时间?
- C#程序运行时间的测试方法
- c++测试程序运行时间的
- linux下计算程序运行时间
- 如何测试程序运行时间?
- C语言之测试程序运行时间
- VS测试程序运行时间
- WinCE下测试程序运行时间的方法
- VC程序运行时间的测试函数