关于花费的时间在两次跑步中有很大差异 [英] About time spent have big difference in two running

查看:70
本文介绍了关于花费的时间在两次跑步中有很大差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< stdio.h>

#include< stdlib.h>

#include< time.h>

int

main(int argc,char * argv [])

{

long i = 10000000L;

clock_t开始,结束;

双倍持续时间;


printf(时间做%ld循环花费,i);

start = clock();

while(i - );

end = clock();


持续时间=(双倍)(结束 - 开始)/ CLOCKS_PER_SEC;

printf("%f seconds \ n",duration);


返回EXIT_SUCCESS;

} / * ----------结束函数main ---------- * /


我运行以上程序,

第一次花费0.031000秒。

我第二次花费0.015000秒

如果我一次又一次地尝试,花费的时间将为0.031或0.015



为什么会有如此大的差异?


谢谢!!!

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

int
main ( int argc, char *argv[] )
{
long i = 10000000L;
clock_t start, end;
double duration;

printf("Time do %ld loop spent ", i);
start = clock();
while (i--);
end = clock();

duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("%f seconds\n", duration);

return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */

I run the above the program,
The first time I spent 0.031000 seconds.
The second time I spent 0.015000 seconds
If I try again and again, the time spent on will 0.031 or 0.015
seconds
Why have such big difference?

thank you!!!

推荐答案

xianwei写道:
xianwei wrote:

#include< stdio.h>

#include< stdlib.h>

#include< time.h>


int

main(int argc, char * argv [])

{

long i = 10000000L;

clock_t start,end;

double持续时间;


printf(时间做%ld循环花费,i);

start = clock();

while(i - );

end = clock();


duration =(double)(end - start)/ CLOCKS_PER_SEC;

printf("%f seconds \ n",duration);


返回EXIT_SUCCESS;

} / * --- -------结束功能主要---------- * /


我运行上面的程序,

我第一次花费0.031000秒。

我第二次花费0.015000秒

如果我一次又一次地尝试,那么时间sp将要0.031或0.015


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

int
main ( int argc, char *argv[] )
{
long i = 10000000L;
clock_t start, end;
double duration;

printf("Time do %ld loop spent ", i);
start = clock();
while (i--);
end = clock();

duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("%f seconds\n", duration);

return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */

I run the above the program,
The first time I spent 0.031000 seconds.
The second time I spent 0.015000 seconds
If I try again and again, the time spent on will 0.031 or 0.015
seconds



你见过一只蜂鸟,并想知道

有多快它的翅膀扑了吗?您可以尝试通过测量一千万次节拍的时间来找到

答案。

所以你设置了你的高精度翼拍计数器,

然后你开始你的计时器:一个沙漏...


-

Eric Sosman
es ***** @ ieee-dot-org.inva 盖子


" xianwei" ; < ba ********* @gmail.coma¨| crit dans le message de news:
11 ******** @ k79g2000hse.googlegroups。 com ...
"xianwei" <ba*********@gmail.coma ¨|crit dans le message de news:
11**********************@k79g2000hse.googlegroups. com...

#include< stdio.h>

#include< stdlib.h>

#include< time.h>


int

main(int argc,char * argv [])

{

long i = 10000000L;

clock_t start,end;

双倍持续时间;


printf(" Time do%ld loop spent',i);

start = clock();

while(i - );

结束=时钟();


持续时间=(双倍)(结束 - 开始)/ CLOCKS_PER_SEC;

printf(" %f秒\ n",duration);


返回EXIT_SUCCESS;

} / * ----------函数结束main ---------- * /


我运行上面的程序,

我第一次花费0.031000秒。 />
我第二次花费0.015000秒

如果我一次又一次地尝试,花费的时间将是0.031或0.015



为什么h ave有这么大的差异?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int
main ( int argc, char *argv[] )
{
long i = 10000000L;
clock_t start, end;
double duration;

printf("Time do %ld loop spent ", i);
start = clock();
while (i--);
end = clock();

duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("%f seconds\n", duration);

return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */

I run the above the program,
The first time I spent 0.031000 seconds.
The second time I spent 0.015000 seconds
If I try again and again, the time spent on will 0.031 or 0.015
seconds
Why have such big difference?



我怀疑你系统上的clock()函数的粒度大约为15

毫秒。如果是这种情况,clock()函数将在每个15毫秒间隔内为所有调用返回

相同的值。根据

,当您在该时间间隔内开始测量时,可以计时持续时间超过15毫秒的任务
。持续0ms或15ms。

类似地,需要15到30之间的时间可能会被转发为

正好是15ms或正好是30ms。


除了这个粒度问题,你应该调查clock()函数。

它是否测量了经过的时间?总处理器时间?你的程序花费在处理器上花费的时间与花在系统上的时间相比?或其他什么甚至......你的

"时间>还会受到计算机执行的其他任务的影响,同时你的程序执行$ ​​b $ b,以及系统的许多其他特性(缓存

内存,与i / o设备共享总线,等等。


为了您的特别关注,我建议您尝试通过这个小循环同步您的时间




clock_t last,start;


last = start = clock();

while(start == last){

start = clock();

}


你应该尝试测量更长的计算,在循环中重复它们

或增加常数。


您应该考虑在Linux中使用更准确的计时功能,例如

非标准gettimeofday。


您应该多次重复测试,并将结果平均,

丢弃极值。


有效的代码分析是*非常*很难。从分析数据中得出结论或制作

的变化也不容易:一个人拥有什么b $ b架构并不一定在另一个架构上,即使只是略微

不同。这个领域没有明确的事实。


-

Chqrlie

I suspect the clock() function on your system has a granularity of around 15
milliseconds. If this is the case, the clock() function will return the
same value for all calls during each 15 millisecond interval. Depending on
when exactly you start your measurements within that interval, a task
lasting cloless than 15ms can be "clocked" as lasting 0ms or 15ms.
Similarly, one that takes between 15 and 30 might be reposted as taking
exactly 15ms or exactly 30ms.

On top of this granularity issue, you should look into the clock() function.
Does it measure elapsed time? total processor time? processor time spent in
your program vs time spent in the system? or something else even... Your
"timings" will also be affected by other tasks the computer performs while
your program executes, and many other characteristics of you system (cache
memory, bus sharing with i/o devices, etc.)

For your peticular concern, I suggest you try and synchronize your timing
efforts with this small loop:

clock_t last, start;

last = start = clock();
while (start == last) {
start = clock();
}

You should try and measure longer computations, by repeating them in a loop
or increasing the constants.

You should consider using more accurate timing functions such as
non-standard gettimeofday in Linux.

You should repeat the tests many many times, and average the results,
discarding extreme values.

Effective code profiling is *very* difficult. Drawing conclusions or making
changes from profiling data is not easy either: what holds on one
architecture does not necessarily on another one, even just slightly
different. There is no definitive truth in this domain.

--
Chqrlie


9月25日,8:15 pm,Eri​​c Sosman< esos ... @ ieee-dot-org.invalidwrote:
On Sep 25, 8:15 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:

你见过一只蜂鸟,想知道吗? />
它的翅膀飞得多快?您可以通过测量一千万次节拍的时间来尝试找到答案。
Have you ever seen a hummingbird, and wondered
how fast its wings flutter? You might try to find
an answer by measuring the time for ten million beats.



谢谢你,你是对的,我应该更换一百万到十几百万美元。

当我那样做,时间保持在0.285 - 0.231秒,我想这个

很好。


测试有多快嗡嗡的翅膀颤动的声音不像一个好的

想法!! :-)

Thank you, you are right, I should replace one millions to ten
millions.
When I do that, the time keep in 0.285 - 0.231 seconds, I think this
is
well.

To test how fast humming wings flutter sound not like a good
idea !! :-)


这篇关于关于花费的时间在两次跑步中有很大差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆