测试程序运行时间的三种方法
来源:互联网 发布:php b2b开源系统 编辑:程序博客网 时间:2024/06/05 09:59
第一种方法
#include <sys/time.h>
#if 0int gettimeofday(struct timeval *tv,struct timezone *tz);
strut timeval {
long tv_sec; /* 秒数 */
long tv_usec; /* 微秒数 */
};
gettimeofday将时间保存在结构tv之中.tz一般我们使用NULL来代替.
#endif
#include <sys/time.h>
#include <stdio.h>
#include <math.h>
void function()
{
unsigned int i,j;
double y;
for(i=0;i<1000;i++)
for(j=0;j<1000;j++)
y=sin((double)i);
}
main()
{
struct timeval tpstart,tpend;
float timeuse;
gettimeofday(&tpstart,NULL);
function();
gettimeofday(&tpend,NULL);
timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+
tpend.tv_usec-tpstart.tv_usec;
timeuse/=1000000;
printf("Used Time:%fn",timeuse);
exit(0);
}
第二种方法
#include <time.h>
//ÄãÒª²âÊԵĎúÂëÔÚÕâÀï
int main(){
int i,j;
clock_t start,end,time;
//GetTickCount(void);
start = clock();
for(i=0;i<10000;i++)
for(j=0;j<100;j++)
;
//GetTickCount(void);
end = clock();
time = end -start;
printf("time = %f\n",time);
return 0;
}
第三种方法
#include<stdio.h>
#include<time.h>
//#include <Windows.h>
#include <sys/types.h>
/*
¿ìËÙÅÅÐòËã·šµÄÁœžöÖ÷Òª²œÖ裬·Öžî£šPartitionºÍQuickSort£©
*/
int Partition(int a[],int low,int high);
void QuickSort(int a[],int low,int high);
void main()
{
int i ;
double start,end,time;
start=GetTickCount();
int a[9]={8,2,3,4,5,6,76,0,334};
QuickSort(a,0,8);
for(i=0;i<9;i++)
printf("%d ",a[i]);
end=GetTickCount();
time=end-start;
printf("%d", time) ;
// return 0;
}
int Partition(int a[],int low,int high)
{
int key=a[low];
while(low<high)
{
while(low<high&&a[high]>=key) high--;
a[low]=a[high];
while(low<high&&a[low]<=key) low++;
a[high]=a[low];
}
return low;
}
void QuickSort(int a[],int low,int high)
{
if(low<high)
{
int p=Partition(a,low,high);
QuickSort(a,p+1,high);
QuickSort(a,low, p-1);
}
}
- 测试程序运行时间的三种方法
- 测试程序运行时间的方法
- C#程序运行时间的测试方法
- 常用的三种获取程序运行时间的方法
- C#计算一段程序运行时间的三种方法
- Matlab中计算程序运行时间的三种方法
- Matlab中计算程序运行时间的三种方法
- Matlab中计算程序运行时间的三种方法
- C#计算一段程序运行时间的三种方法
- Matlab中计算程序运行时间的三种方法
- C#计算程序运行时间的三种方法
- Matlab中计算程序运行时间的三种方法
- C#计算一段程序运行时间的三种方法
- 三种计算c#程序运行时间的方法
- C#计算一段程序运行时间的三种方法
- Matlab中计算程序运行时间的三种方法
- 测试程序运行的时间
- 测试程序的运行时间
- 补码 的运算
- VMWare 下OEM系统激活问题
- flex 和java数据类型匹配关系
- H.264 JM18.0 代码及算法分析:I帧编码续1 ——I16MB模式编码
- Android深入浅出之Audio 第一部分 AudioTrack分析
- 测试程序运行时间的三种方法
- [android反编译小结]apktool/ AXMLPrinter2.jar/ dex2jar.bat/ jd-gui/ Jodeclipse/ JadClipse
- oracle数据类型
- yaourt 查询时Segmentation fault package-query问题解决
- PC端与android手机端使用adb forword通信
- eclipse中tomcat启动后在浏览器中无法访问tomcat的欢迎页面,但是能访问工程
- Oracle 数据库的表空间和 Oracle 数据库数据文件
- Android深入浅出之Surface
- poj 1011 sticks Memory:44K Time:93MS