测试代码运行时间

ssmtw 2011-03-20 04:59:35
我想测试下代码运行时间,怎么测呀,谁知道?
...全文
343 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
moxueling 2011-09-11
  • 打赏
  • 举报
回复
GetTickCount() 每隔15.5毫秒才更新一次时间,所以你测试出来是0
驴被脑袋踢了 2011-03-20
  • 打赏
  • 举报
回复
clock
gettickcount
c_losed 2011-03-20
  • 打赏
  • 举报
回复
引用
for(i=0;i<9;i++)


时间为0 是因为9太小了。。。设置大点 比如1000 就能看到效果了
c_losed 2011-03-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ssmtw 的回复:]


#include<stdio.h>
#include<time.h>
#include <Windows.h>
/*
快速排序算法的两个主要步骤,分割(Partition和QuickSort)
*/
int Partition(int a[],int low,int high);
void QuickSort(int a[],int low……
[/Quote]

debug调试看下
反正我单步调的时候 不是0
ssmtw 2011-03-20
  • 打赏
  • 举报
回复
我用了固定的随机数,应该是一样的,为什么每次结果不一样?
失散糖 2011-03-20
  • 打赏
  • 举报
回复
QueryPerformanceFrequency 获取计数频率
QueryPerformanceCounter 用获取计数值, 用两次求差, 这个比较精确,虽然说不清这到底算是什么时间

rabbitjerry 2011-03-20
  • 打赏
  • 举报
回复
补充:
GetTickCount函数在windows.h头文件中
clock函数在time.h头文件中
rabbitjerry 2011-03-20
  • 打赏
  • 举报
回复
两种方法:

1 使用GetTickCount()函数。
例如:
int tb = GetTickCount();
// your code
int te = GetTickCount();
int t = te - tb;
t就是代码运行的时间,单位是毫秒。

2 使用clock函数
clock_t tb = GetTickCount();
// your code
clock_ te = GetTickCount();
clock_t t = te - tb;
这里的t的单位是时钟周期。如果要换算成毫秒。需要:
t = (te-tb)*1000/CLOCKS_PER_SEC;
其中的CLOCKS_PER_SEC表示每秒钟有多少个时钟周期。


a5511258 2011-03-20
  • 打赏
  • 举报
回复
输出时间按double或是float格式输出
bluewanderer 2011-03-20
  • 打赏
  • 举报
回复
才9个数排序肯定用不了1毫秒啊...
ssmtw 2011-03-20
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<time.h>
#include <Windows.h>
/*
快速排序算法的两个主要步骤,分割(Partition和QuickSort)
*/
int Partition(int a[],int low,int high);
void QuickSort(int a[],int low,int high);
void main()
{
int i ;
DWORD 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);
}
}

不行呀,还是输出0
c_losed 2011-03-20
  • 打赏
  • 举报
回复
ps:
GetTickCount() 返回的单位是 毫秒
c_losed 2011-03-20
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<time.h>
#include <Windows.h>
/*
快速排序算法的两个主要步骤,分割(Partition和QuickSort)
*/
int Partition(int a[],int low,int high);
void QuickSort(int a[],int low,int high);
void main()
{
int i ;
DWORD 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);
}
}
ssmtw 2011-03-20
  • 打赏
  • 举报
回复
如我想测试下面的代码,为什么测试结果为0,应该怎么改
#include<stdio.h>
#include<time.h>
/*
快速排序算法的两个主要步骤,分割(Partition和QuickSort)
*/
int Partition(int a[],int low,int high);
void QuickSort(int a[],int low,int high);
void main()
{
int i ;
clock_t start,end,time;
start=clock();
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=clock();
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);
}
}
witwolf 2011-03-20
  • 打赏
  • 举报
回复
代码运行开始获取一次
代码运行玩获取一次
两次相减
clock_t start,end,time;
start=clock();
...
end=clock();
time=end-start;
KID_coder 2011-03-20
  • 打赏
  • 举报
回复
DWORD GetTickCount( void );
获取时间
代码运行开始获取一次
代码运行玩获取一次
两次相减
zhangsongcui 2011-03-20
  • 打赏
  • 举报
回复
t=clock();
/*...*/
clock()-t;

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧