clock()函数 [英] clock() function

查看:90
本文介绍了clock()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用clock()来计算我的部分代码

例如

clk1 = clock();

/ *代码* /

clk2 =时钟();

/ *以秒计算时间* /

......

clk1 =时钟();

/ *代码* /

clk2 =时钟();

/ *计算时间在几秒钟* /


我必须多做几次。我很担心看完时钟()的

手册页后说:

USAGE

clock()返回的值是以微秒为单位定义

与CPU时钟具有更高分辨率的b / b
系统的兼容性。因此,返回的值将在累积仅2147秒的CPU时间后回收,即
(约36分钟)。


这是否意味着我的代码运行超过36分钟。时间计算

会出错吗?

I''m using clock() to time parts of my code
e.g.
clk1 = clock();
/* code */
clk2 = clock();
/* calculate time in secs */
......
clk1 = clock();
/* code */
clk2 = clock();
/* calculate time in secs */

I have to do this a couple of more times. I''m worried after reading the
man page for clock() which says:
USAGE
The value returned by clock() is defined in microseconds for
compatibility with systems that have CPU clocks with much
higher resolution. Because of this, the value returned will
wrap around after accumulating only 2147 seconds of CPU time
(about 36 minutes).

Does this mean if my code runs more than 36 mins. the timings calculated
will be wrong?

推荐答案



" Pushkar Pradhan < PU ***** @ gri.msstate.edu>在留言中写道

新闻:3F ************** @ gri.msstate.edu ...

"Pushkar Pradhan" <pu*****@gri.msstate.edu> wrote in message
news:3F**************@gri.msstate.edu...
我正在使用clock()来计算我的代码的部分时间
例如
clk1 = clock();
/ * code * /
clk2 = clock();
/ * calculate时间以秒为单位* /
.....
clk1 =时钟();
/ *代码* /
clk2 =时钟();
/ *计算时间以秒为单位* /

我必须多做几次。我在阅读时钟()的手册后感到很担心:
使用
clock()返回的值定义为微秒,以便与系统兼容CPU时钟具有更高的分辨率。因此,返回的值将在累积仅2147秒的CPU时间后(或大约36分钟)回收。

这是否意味着如果我的代码运行超过36分钟。计算的时间将是错误的?
I''m using clock() to time parts of my code
e.g.
clk1 = clock();
/* code */
clk2 = clock();
/* calculate time in secs */
.....
clk1 = clock();
/* code */
clk2 = clock();
/* calculate time in secs */

I have to do this a couple of more times. I''m worried after reading the
man page for clock() which says:
USAGE
The value returned by clock() is defined in microseconds for
compatibility with systems that have CPU clocks with much
higher resolution. Because of this, the value returned will
wrap around after accumulating only 2147 seconds of CPU time
(about 36 minutes).

Does this mean if my code runs more than 36 mins. the timings calculated
will be wrong?




我理解它是说你的价值类型

标准库实现''''时钟()''

函数(类型''clock_t'')只能代表时间间隔

最多约36分钟之前"包裹"从它的
最大值回到零,并重新开始计数。


具体的基础类型和涉及的价值

是实现定义。我使用你的作为你的实施的缩写

下面:


这意味着你的''clock()''函数的分辨率

加上你的类型''clock_t''结果的范围在

a最大范围为2147秒。


这表明你的''clock_t''类型是32位

整数类型,使用31''value''位,一位用于表示-1''错误值''的

符号,以及''CLOCKS_PER_SEC''

宏定义为1000000(百万),因为31位代表的最高

值是2147483647,而

2147483647/1000000 == 2147 (使用整数除法)。


这与上面关于''
微秒中定义''的说法一致。''2147/60 == 35.78,或者约36分钟。


正如我所说,这些东西的具体细节是实施

定义,以上就是我的扣除。为确定确切的限制和行为确定

,你应该写一个小的测试程序,使用''sizeof(clock_t)'',

''CHAR_BIT''和''CLOCKS_PER_SEC''确定你的实现使用的确切

值。


我相信你的情况如上测试程序可能只会是学术性的,但是,因为:


关于你的具体问题:


如果你将经过的时间存储在''clock_t''

对象中,然后是,你的实现将这个值限制在大约36分钟之前包裹返回

为零。


但是你不需要将值存储在''clock_t''

对象的类型中。使用范围足够大的类型,以满足我们预期的需求,例如:输入''double''。所需的

类型''double''显着高于

31 bit整数值。


double start =(double)clock();

double elapsed = 0;


/ * etc * /


elapsed =(double)clock() - start;

HTH,

-Mike




I understand it to be saying that the type of the value
returned by your standard library implementation''s ''clock()''
function (type ''clock_t'') can only represent time intervals
of up to approximately 36 minutes before "wrapping" from its
maximum value back to zero, and start counting up again.

The specific underlying types and the values involved
are implementation defined. I use "your" as abbreviation
for "your implementation''s" below:

This means that the resolution of your ''clock()'' function
combined with the range of your type ''clock_t'' results in
a maximum range of 2147 seconds.

This suggests to me that your ''clock_t'' type is a 32-bit
integer type, using 31 ''value'' bits, and one bit to indicate the
sign for the -1 ''error value'', and that your ''CLOCKS_PER_SEC''
macro is defined as 1000000 (one million), since the highest
value representable with 31 bits is 2147483647, and
2147483647 / 1000000 == 2147 (using integer division).

This agrees with the statement above about ''defined in
microseconds.'' 2147 / 60 == 35.78, or about 36 minutes.

As I say, the specifics of this stuff is implementation
defined, and the above is my deduction. To determine
for sure the exact limitations and behavior, you should
write a small test program that uses ''sizeof(clock_t)'',
''CHAR_BIT'', and ''CLOCKS_PER_SEC'' to determine the exact
values your implementation uses.

I believe in your case the above test program would probably
be only academic, though, because:

About your specific question:

If you store your elapsed time in a type ''clock_t''
object then yes, your implementation limits this value
to about 36 minutes before it "wraps" around back
to zero.

But you need not store the value in a type ''clock_t''
object. Use a type with a range large enough for
our anticipated needs, e.g. type ''double''. The required
range of type ''double'' is significantly higher than
that of a 31 bit integer value.

double start = (double)clock();
double elapsed = 0;

/* etc */

elapsed = (double)clock() - start;
HTH,
-Mike





" Mike Wahler" < MK ****** @ mkwahler.net> schrieb im Newsbeitrag

新闻:tF **************** @ newsread4.news.pas.earthli nk.net ...

"Mike Wahler" <mk******@mkwahler.net> schrieb im Newsbeitrag
news:tF****************@newsread4.news.pas.earthli nk.net...

Pushkar Pradhan < PU ***** @ gri.msstate.edu>在消息中写道
新闻:3F ************** @ gri.msstate.edu ...

"Pushkar Pradhan" <pu*****@gri.msstate.edu> wrote in message
news:3F**************@gri.msstate.edu...
我正在使用clock()来我的代码的时间部分
例如
clk1 = clock();
/ * code * /
clk2 = clock();
/ *以秒计算时间* /
.....
clk1 = clock();
/ * code * /
clk2 = clock();
/ *以秒计算时间* /

我必须多做几次。我在阅读时钟()的手册后感到很担心:
使用
clock()返回的值定义为微秒,以便与系统兼容CPU时钟具有更高的分辨率。因此,返回的值将在累积仅2147秒的CPU时间后(或大约36分钟)回收。

这是否意味着如果我的代码运行超过36分钟。计算的时间将是错误的?
I''m using clock() to time parts of my code
e.g.
clk1 = clock();
/* code */
clk2 = clock();
/* calculate time in secs */
.....
clk1 = clock();
/* code */
clk2 = clock();
/* calculate time in secs */

I have to do this a couple of more times. I''m worried after reading the
man page for clock() which says:
USAGE
The value returned by clock() is defined in microseconds for
compatibility with systems that have CPU clocks with much
higher resolution. Because of this, the value returned will
wrap around after accumulating only 2147 seconds of CPU time
(about 36 minutes).

Does this mean if my code runs more than 36 mins. the timings calculated
will be wrong?




[....]

但你不需要将值存储在一个输入''clock_t''
对象。使用范围足够大的类型以满足我们的预期需求,例如:输入''double''。所需的类型''double''范围明显高于31位整数值。

double start =(double)clock();
double elapsed = 0;

/ * etc * /

elapsed =(double)clock() - start;



[....]

But you need not store the value in a type ''clock_t''
object. Use a type with a range large enough for
our anticipated needs, e.g. type ''double''. The required
range of type ''double'' is significantly higher than
that of a 31 bit integer value.

double start = (double)clock();
double elapsed = 0;

/* etc */

elapsed = (double)clock() - start;



"时钟函数返回实现的最佳近似值

处理器
自实现定义开始以来程序使用的
时间

时代相关

只对程序调用。要确定以秒为单位的时间,

返回的值时钟函数应该除以宏的值
CLOCKS_PER_SEC。如果

使用的处理器时间不可用或其值无法表示,

函数

^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^

返回值(clock_t)-1"


如果我没有弄错的话,来自clock()的值_returned_将会(可能,请参阅下面的
)换行,所以将它存储在一个双倍中不会有帮助,并且期限为OP

可以使用甚至可能比这36分钟少得多,因为第一次调用

到clock()很可能会返回一个值> 0.

如果在clock_t中可以表示的最大

值超出标准,我在标准中找不到任何内容,所以我不确定是否

值实际上将包含在每个实现上,但是上面标准'的描述中的最后一句话告诉我,在这种情况下

返回值可能是-1。 (或甚至_required_返回-1 ...不确定

来自措辞)。


亲切的问候

Robert



"The clock function returns the implementation?s best approximation to the
processor
time used by the program since the beginning of an implementation-defined
era related
only to the program invocation. To determine the time in seconds, the value
returned by
the clock function should be divided by the value of the macro
CLOCKS_PER_SEC. If
the processor time used is not available or its value cannot be represented,
the function
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
returns the value (clock_t)-1"

If I am not mistaken, the value _returned_ from clock() will (maybe, see
below) wrap, so storing that in a double won''t help, and the period the OP
can use may even be much less than these 36 minutes, because the first call
to clock() will most likely return a value > 0.
I could not find anything in the standard about what happens if the maximum
value representable in a clock_t is exceeded so I am not sure whether the
value will actually wrap on every implementation, but the last sentence in
the standard''s description above suggests to me, that in this case the
return value may be -1. (or is even _required_ to return -1... not sure
about that from the wording).

kind regards
Robert


" Mike Wahler" < MK ****** @ mkwahler.net>在消息中写道

news:tF **************** @ newsread4.news.pas.earthli nk.net ...
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:tF****************@newsread4.news.pas.earthli nk.net...

Pushkar Pradhan < PU ***** @ gri.msstate.edu>在消息中写道
新闻:3F ************** @ gri.msstate.edu ...

"Pushkar Pradhan" <pu*****@gri.msstate.edu> wrote in message
news:3F**************@gri.msstate.edu...
我正在使用clock()来我的代码的时间部分
例如
clk1 = clock();
/ * code * /
clk2 = clock();
/ *以秒计算时间* /
.....
clk1 = clock();
/ * code * /
clk2 = clock();
/ *以秒计算时间* /

我必须多做几次。我在阅读时钟()的手册后感到很担心:
使用
clock()返回的值定义为微秒,以便与系统兼容CPU时钟具有更高的分辨率。因此,返回的值将在累积仅2147秒的CPU时间后(或大约36分钟)回收。

这是否意味着如果我的代码运行超过36分钟。计算的时间将是错误的?
I''m using clock() to time parts of my code
e.g.
clk1 = clock();
/* code */
clk2 = clock();
/* calculate time in secs */
.....
clk1 = clock();
/* code */
clk2 = clock();
/* calculate time in secs */

I have to do this a couple of more times. I''m worried after reading the
man page for clock() which says:
USAGE
The value returned by clock() is defined in microseconds for
compatibility with systems that have CPU clocks with much
higher resolution. Because of this, the value returned will
wrap around after accumulating only 2147 seconds of CPU time
(about 36 minutes).

Does this mean if my code runs more than 36 mins. the timings calculated
will be wrong?



我理解它是说标准库实现'''返回的值的类型
clock()''
函数(类型''clock_t'')只能表示在换行之前最多约36分钟的时间间隔。从其最大值回到零,并重新开始计数。

具体的基础类型和涉及的值是实现定义的。我使用你的作为您的实施的缩写
下面:

这意味着你的''clock()''函数的分辨率
与你的类型''clock_t''的范围相结合会产生最大范围2147秒。

这告诉我你的''clock_t''类型是一个32位的整数类型,使用31''值''位,一位表示
签署-1''错误值'',并且你的''CLOCKS_PER_SEC''
宏定义为1000000(百万),因为最高的
值可用31表示位是2147483647,并且
2147483647/1000000 == 2147(使用整数除法)。

这与上面关于''在微秒中定义的'的陈述一致。''2147 / 60 == 35.78,或大约36分钟。

正如我所说,这些东西的具体细节是实施
定义,以上就是我的推论。要确定确切的限制和行为,你应该编写一个小的测试程序,使用''sizeof(clock_t)'',
''CHAR_BIT''和''CLOCKS_PER_SEC ''确定你的实现使用的确切值。

我相信你的情况,上述测试程序可能只是学术性的,但是,因为:

关于您的具体问题:

如果您将经过的时间存储在类型''clock_t''
对象中,那么是的,您的实现将此值限制为大约在它包裹之前36分钟在后面
到零。

但你不需要将值存储在类型''clock_t''
对象中。使用范围足够大的类型以满足我们的预期需求,例如:输入''double''。所需的类型''double''范围明显高于31位整数值。

double start =(double)clock();
double elapsed = 0;

/ * etc * /

elapsed =(double)clock() - start;



I understand it to be saying that the type of the value
returned by your standard library implementation''s ''clock()''
function (type ''clock_t'') can only represent time intervals
of up to approximately 36 minutes before "wrapping" from its
maximum value back to zero, and start counting up again.

The specific underlying types and the values involved
are implementation defined. I use "your" as abbreviation
for "your implementation''s" below:

This means that the resolution of your ''clock()'' function
combined with the range of your type ''clock_t'' results in
a maximum range of 2147 seconds.

This suggests to me that your ''clock_t'' type is a 32-bit
integer type, using 31 ''value'' bits, and one bit to indicate the
sign for the -1 ''error value'', and that your ''CLOCKS_PER_SEC''
macro is defined as 1000000 (one million), since the highest
value representable with 31 bits is 2147483647, and
2147483647 / 1000000 == 2147 (using integer division).

This agrees with the statement above about ''defined in
microseconds.'' 2147 / 60 == 35.78, or about 36 minutes.

As I say, the specifics of this stuff is implementation
defined, and the above is my deduction. To determine
for sure the exact limitations and behavior, you should
write a small test program that uses ''sizeof(clock_t)'',
''CHAR_BIT'', and ''CLOCKS_PER_SEC'' to determine the exact
values your implementation uses.

I believe in your case the above test program would probably
be only academic, though, because:

About your specific question:

If you store your elapsed time in a type ''clock_t''
object then yes, your implementation limits this value
to about 36 minutes before it "wraps" around back
to zero.

But you need not store the value in a type ''clock_t''
object. Use a type with a range large enough for
our anticipated needs, e.g. type ''double''. The required
range of type ''double'' is significantly higher than
that of a 31 bit integer value.

double start = (double)clock();
double elapsed = 0;

/* etc */

elapsed = (double)clock() - start;



再次审查ISO标准后,我对之前的结论的信心较小。事实上,事实上,我想的越多,我就不会认为

它是有效的。

以下是标准关于''clock()''的所有内容:

(具体来说,注意7.23.1 / 4)


<开始ISO 9899报价>


7.23日期和时间< time.h>


7.23.1时间成分< br $>

[...]

2定义的宏为NULL(在7.17中描述);并且


CLOCKS_PER_SEC


扩展为类型为clock_t的常量表达式(在下面描述

)即$ / b $ b时钟函数返回值的每秒数。


3声明的类型是size_t(在7.17中描述);


clock_t




time_t


是能够代表时间的算术类型;并且


struct tm


包含日历时间的组成部分,称为

分解时间。


4 clock_t中可表示的时间范围和精度
和time_t是实现定义的。


[...]


7.23.2.1时钟功能


概要


1#包括< time.h>

clock_t clock(无效);


描述


2时钟功能确定使用的处理器时间。


返回


3时钟函数返回实现的最佳近似值

从程序使用的处理器时间开始的

实现定义的时代仅与程序调用有关。

要确定以秒为单位的时间,返回的值按时钟

函数应除以宏CLOCKS_PER_SEC的值。

如果进程或者使用的时间不可用或者其值不能表示为
,该函数返回值(clock_t)( - 1)。 (266)


(266)为了衡量一个程序所花费的时间,应该在程序开始时调用时钟

函数,从后续

电话返回的值中减去
的返回值。


< end ISO 9899 quote>

对比度(从上面):


4 clock_t中可表示的时间范围和精度

和time_t是实现定义的。 />

引用您的手册​​页:


" ...因此,[clock()]返回的值将<在积累了2147秒的CPU时间后,
回绕了

(约36分钟)。


目前尚不清楚我是否只返回实际值

将换行,并且内部使用更大范围

by clock(),或者如果使用的内部范围具有所述

限制。


ISO标准同样含糊不清提到



clock_t和time_t中可表示的时间范围和精度,没有说明类型

clock()内部使用在调用之间保持时间

或该类型的范围。


7.23.2.1 / 3谈论实施定义的时代
但似乎没有说明使用了什么计量单位

来指定这个时代。我假设它也属于

''实现定义''。

因此缺乏(imo)关于此的明确知识,它看起来像

就像我们需要一个算法来计算

值每次包裹的程度,并进行调整,

累积调整后的价值或者使用其他一些

测量工具,其范围足以满足您的需求。

''time()''可能会提供更大的范围,但可能是

也是一个较小的分辨率,所以如果你不需要''clock()''提供的分辨率

可能是一个选项。


也许其他人可以给出更明确的答案

并解决我上面提到的不确定性。


也许我现在太累了,根本无法看到明显的,这不是第一次。 :-)


-Mike


After again reviewing the ISO standard, I have less
confidence in my previous conclusion. As a matter
of fact, the more I think about it, I don''t think
it''s valid at all.

Here is everything the standard has to say about ''clock()'':
(specifically, note 7.23.1 / 4)

<begin ISO 9899 quote>

7.23 Date and time <time.h>

7.23.1 Components of time

[...]

2 The macros defined are NULL (described in 7.17); and

CLOCKS_PER_SEC

which expands to a constant expression with type clock_t (described
below) that is the number per second of the value returned by the
clock function.

3 The types declared are size_t (described in 7.17);

clock_t
and

time_t

which are arithmetic types capable of representing times; and

struct tm

which holds the components of a calendar time, called the
broken-down time.

4 The range and precision of times representable in clock_t
and time_t are implementation-defined.

[...]

7.23.2.1 The clock function

Synopsis

1 #include <time.h>
clock_t clock(void);

Description

2 The clock function determines the processor time used.

Returns

3 The clock function returns the implementation?s best approximation
to the processor time used by the program since the beginning of an
implementation-defined era related only to the program invocation.
To determine the time in seconds, the value returned by the clock
function should be divided by the value of the macro CLOCKS_PER_SEC.
If the processor time used is not available or its value cannot be
represented, the function returns the value (clock_t)(-1). (266)

(266) In order to measure the time spent in a program, the clock
function should be called at the start of the program and its
return value subtracted from the value returned by subsequent
calls.

<end ISO 9899 quote>
Contrast (from above):

4 The range and precision of times representable in clock_t
and time_t are implementation-defined.

with the quotation of your man page:

"...Because of this, the value returned [by clock()] will
wrap around after accumulating only 2147 seconds of CPU time
(about 36 minutes)."

It is not clear to me whether only actual value returned
will wrap, and that some larger range is internally used
by clock(), or if the internal range used has the stated
limitation.

The ISO standard is imo equally vague, only mentioning
"the range and precision of times representable in
clock_t and time_t", saying nothing about the type
clock() uses internally to keep time between invocations
or the range of that type.

7.23.2.1 / 3 talks about an "implementation-defined era"
but does not seem to say what unit of measurement is used
to specify this "era". I''m assuming that falls under
''implementation-defined'' as well.
So lacking (imo) definitive knowledge about this, it looks
like we''d need an algorithm to figure out how far the
value has ''wrapped'' each time, and make adjustments,
accumulating the adjusted values. Or use some other
measurement tool with a range sufficient for your needs.
''time()'' probably gives a much larger range, but probably
also a lesser resolution, so if you don''t need the resolution
provided by ''clock()'' that might be an option.

Perhaps someone else can give a more definitive answer
and address my uncertainties stated above.

Maybe I''m just too tired right now, and simply cannot
see the obvious, it wouldn''t be the first time. :-)

-Mike


这篇关于clock()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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