C语言如何 计算程序运行时间?

justin_0009 2008-07-27 09:13:40
#include <stdio.h>

如何计算hello world的运行时间?

int main()
{
while(10000)
printf("hello world\n");

return 0;
}
...全文
7497 37 打赏 收藏 转发到动态 举报
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2011-11-01
  • 打赏
  • 举报
回复
提醒:因为CPU负荷影响电脑计时器,所以有时循环执行待测程序段比如100000遍手动掐秒表计时更准确。
jiamianwuzhe 2011-11-01
  • 打赏
  • 举报
回复
学习了,以前学的实在是太浅了
起个昵称真难 2011-03-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 iu_81 的回复:]

struct tm sttime,fitime;
_getsystime(&sttime);
....
_getsystime(&fitime);
[/Quote]

Roy_Smiling 2011-03-09
  • 打赏
  • 举报
回复
int main(){
time1=now time;
.....
time2=now time;
time-time2-time1;
return 0;
}
AnYidan 2011-03-09
  • 打赏
  • 举报
回复
有意义吗?

按3楼的说法,精确到 毫秒,执行一次 printf("hello, world")的精度够吗?

楼主执行10000次,然后平均,谁能保证 windows 不插入其他进程?

求解?

个人认为精确的执行时间只能在 适时系统中?
kobe198702 2011-03-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hai040 的回复:]

...
while(10000)
printf("hello world\n");
你想执行10000次?
[/Quote]
while(10000) 不是执行10000次的意思吧,就是简单的为真
baiguangyu001 2011-03-08
  • 打赏
  • 举报
回复
感谢三楼!!
acmxiaocao 2010-11-29
  • 打赏
  • 举报
回复
我是来谢3楼的……
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hai040 的回复:]
...
while(10000)
printf("hello world\n");
你想执行10000次?
[/Quote]

他想不停下来!
dfatfat 2010-10-30
  • 打赏
  • 举报
回复
开头加入 <time.h>
#include <time.h>
int main()
{
clock_t start, finish; //这是以微妙计时的所以后面要除以1000
start = clock();
int i=10000;
while(i--){
printf("hello\n");
}
finish =clock();
(double)(finish - start) /1000;/*这是毫秒除以1000转换为秒*/
printf("%f seconds\n",(double)(finish - start) /1000);
getchar();
return 0;
}
没有比这个更简单的了
sunshineyy85 2010-10-30
  • 打赏
  • 举报
回复
不知怎样才能准确计算出来呢
caoyangnianhua2007 2010-10-14
  • 打赏
  • 举报
回复
linux下面不是可以这样来吗?

gcc -Wall -pg calllll.c
gprof a.out
千杯不醉-sen 2010-10-14
  • 打赏
  • 举报
回复

printf( "%lf seconds\n", duration );
Time to do 10000000 empty loops is 0.025000 seconds
千杯不醉-sen 2010-10-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hellodan 的回复:]
C/C++ code

C/C++中的计时函数是clock(),而与其相关的数据类型是clock_t。在MSDN中,查得对clock函数定义如下:

clock_t clock( void );

这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock)。其中clock_t……
[/Quote]
printf( "%lf seconds\n", duration );
Time to do 10000000 empty loops is 0.030000 seconds
千杯不醉-sen 2010-10-14
  • 打赏
  • 举报
回复
同样的数字,有时差的。

#include <stdio.h>
#include <conio.h>
#include <time.h>

void sleep( clock_t wait );

void main( void )
{
long i = 1000000000L;
clock_t start, finish;
double duration;

printf( "Delay for two seconds\n" );
sleep(2000);
printf( "Done!\n" );

printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- ) ;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%lf seconds\nOver!\n", duration );
getch();
}

void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() ) ;
}
///////////////////////////////////////////////////////////////////
Delay for two seconds
Done!
Time to do 1000000000 empty loops is 2.510000 seconds
Over!
Delay for two seconds
Done!
Time to do 1000000000 empty loops is 2.505000 seconds
Over!
Delay for two seconds
Done!
Time to do 1000000000 empty loops is 2.485000 seconds
Over!
dfatfat 2010-10-13
  • 打赏
  • 举报
回复
#include <stdio.h>

#include<time.h>
int main()
{
clock_t start,finish;
start=clock();
while(10000)
printf("hello world\n");
finish=clock();
printf("%f s",(double)(start-finish)/1000);//转换为秒数
getchar();
return 0;
}
lly212 2010-10-13
  • 打赏
  • 举报
回复
#incldue <time.h>
time_t begin = time();

// operating

time_t end = time();

end - begin//
qmycy 2010-10-13
  • 打赏
  • 举报
回复
不错,学习下
try325 2010-10-13
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 csuxiaowu 的回复:]

引用 7 楼 hai040 的回复:

...
while(10000)
printf("hello world\n");
你想执行10000次?

++
[/Quote]
呵呵
Csuxiaowu 2010-10-13
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hai040 的回复:]

...
while(10000)
printf("hello world\n");
你想执行10000次?
[/Quote]
++
加载更多回复(18)

69,371

社区成员

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

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