测试代码运行时间

ssmtw 2011-03-20 04:59:35
我想测试下代码运行时间,怎么测呀,谁知道?
...全文
352 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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;
内容概要:本文介绍了软件定义汽车(SDV)的最佳实践案例,重点围绕基于Vector技术的电子电气(E/E)架构设计与实现。文档展示了高算力计算平台(HPC)、区域控制器(Zone ECU)和车载网络(如CAN、Ethernet)的系统架构布局,并结合AUTOSAR操作系统(Classic/Adaptive)、虚拟化(Hypervisor)和SOA服务设计,构建现代化车载系统。通过vCANdrive平台演示了从开发、测试(SIL/HIL)、到OTA升级的全流程,涵盖传感器、执行器、应用层软件及云端协同的集成方案。同时展示了硬件原型(如树莓派、Triboard)和MICROSAR系列工具链在实际项目中的应用。; 适合人群:从事汽车电子系统开发、车载软件架构设计以及智能网联汽车研发的工程师和技术管理人员,具备一定的嵌入式系统或AUTOSAR基础者更佳。; 使用场景及目标:①理解软件定义汽车的整体架构设计方法;②掌握基于Vector工具链的HPC与区域控制器集成方案;③实现OTA更新、SIL/HIL测试、ETH-CAN通信转换等关键技术验证;④支持智能驾驶(ADAS)与智能座舱(IVI)系统的快速原型开发。; 阅读建议:建议结合Vector相关工具(如PREEvision、CANoe4SW、MICROSAR)进行实践操作,重点关注系统分层设计、通信机制与软件更新流程,同时可参考文档中的硬件连接示意图与信号映射关系进行仿真与实车验证。

70,026

社区成员

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

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