clock_t clock() ambiguity

M

Matt

Does clock_t clock() measure real time or process time? Unless I have
missed something, Stroustrup's treatment in TC++PL (section D.4.4.1) is
none too clear on that question.

Is clock() well-implemented for the major compilers g++, VC++, and
Macintosh compilers and other C++ compilers on Linux/Unix, Windows, and
MacOS?
 
K

Kai-Uwe Bux

Matt said:
Does clock_t clock() measure real time or process time? Unless I have
missed something, Stroustrup's treatment in TC++PL (section D.4.4.1) is
none too clear on that question.

C++ inherits clock() from the standard C header time.h. The publicly
available draft of the C standard says:

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.

Is clock() well-implemented for the major compilers g++, VC++, and
Macintosh compilers and other C++ compilers on Linux/Unix, Windows, and
MacOS?

I don't know, and I think it is not topical in this group.


Best

Kai-Uwe Bux
 
P

Phlip

Matt said:
Does clock_t clock() measure real time or process time? Unless I have
missed something, Stroustrup's treatment in TC++PL (section D.4.4.1) is
none too clear on that question.

Is clock() well-implemented for the major compilers g++, VC++, and
Macintosh compilers and other C++ compilers on Linux/Unix, Windows, and
MacOS?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_clock.asp

Note the compatibility is "ANSI", which is MS's code for the "ISO C
Standard". All compilers worth using will have it.

Then note the first line is "Calculates the wall-clock time used by the
calling process." That "wall-clock" is jargon for real time, not process
time. And "used by the calling process" means the "epoch" starts when the
program starts.

In future, please hit a question with Google before posting it! I answered
all your questions with the first citation for [msdn clock_t].
 
M

mlimber

Matt said:
Does clock_t clock() measure real time or process time? Unless I have
missed something, Stroustrup's treatment in TC++PL (section D.4.4.1) is
none too clear on that question.

IIRC, it's not defined by the C or C++ standards. Neither language has
the inherent concept of real time vs. process time, so implementations
will vary from platform to platform.
Is clock() well-implemented for the major compilers g++, VC++, and
Macintosh compilers and other C++ compilers on Linux/Unix, Windows, and
MacOS?

Depends on what you mean by "well-implemented." The function is
required by the standard, yes, but does it function the same
everywhere? No.

Cheers! --M
 
P

Phlip

Kai-Uwe Bux said:
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.

Oh-kay. The MSDN page I cited claimed "wall-clock" time. Processor time is
available, inside Win32, but not used. The page did not claim the function
returns (clock_t)-1, despite the processor time is not available to the
program.

Is there a language lawyer in the house??
I don't know, and I think it is not topical in this group.

It's not topical on any other group, either, by the same logic.
 
D

Default User

Phlip said:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib
/html/_crt_clock.asp

Note the compatibility is "ANSI", which is MS's code for the "ISO C
Standard". All compilers worth using will have it.

Then note the first line is "Calculates the wall-clock time used by
the calling process." That "wall-clock" is jargon for real time, not
process time. And "used by the calling process" means the "epoch"
starts when the program starts.

That's not what it says, and that's not right.

"The clock function tells how much time the calling process has used."

It is not the wall time, it's the process time.
In future, please hit a question with Google before posting it! I
answered all your questions with the first citation for [msdn
clock_t].


Unfortunately, you answered them incorrectly.



Brian
 
D

Default User

mlimber said:
IIRC, it's not defined by the C or C++ standards. Neither language has
the inherent concept of real time vs. process time, so implementations
will vary from platform to platform.

From the C99 draft standard:

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.252)

252In 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.
Depends on what you mean by "well-implemented." The function is
required by the standard, yes, but does it function the same
everywhere? No.

No, but it should be reasonably close. Unlike tine(), the semantics for
clock() are fairly well defined. It does waffle a bit, with "best
approximation", but it's not unspecified like time() and time_t.




Brian
 
P

Phlip

Default said:
"The clock function tells how much time the calling process has used."

It is not the wall time, it's the process time.

Despite, uh, saying "wall" time at the top of the page?

Please Google [wall-clock time]. If it's meaning isn't related to the
movement of a clock's hands while the program runs, then I'm wrong.
Otherwise, the MSDN page is inconsistent with itself.

A Win32 test involving Sleep() and two clock() calls would instantly answer
questions about the actual implementation.

And please be more careful with your own claims.
 
D

Default User

Phlip said:
Despite, uh, saying "wall" time at the top of the page?

It's poorly (or incorrectly) worded in that section. The Remarks and
the example code get it right.
Please Google [wall-clock time]. If it's meaning isn't related to the
movement of a clock's hands while the program runs, then I'm wrong.

I know what it means, but that not what clock() measures. That's what
time() measures.
Otherwise, the MSDN page is inconsistent with itself.

Good thing it's not the Standard.
A Win32 test involving Sleep() and two clock() calls would instantly
answer questions about the actual implementation.

And please be more careful with your own claims.

As you're incorrect, I'd watch the atty-tude.



Brian
 
K

Kai-Uwe Bux

Phlip said:
Oh-kay. The MSDN page I cited claimed "wall-clock" time.

If so, that page is mistaken or describes a non-compliant implementation of
clock(). Pick your poison.

Processor time is available, inside Win32, but not used.

Huh? And that relates to the clock() function in C or C++ like how?

The page did not claim the function returns (clock_t)-1, despite the
processor time is not available to the program.

Is there a language lawyer in the house??

I think, no lawyer is needed. The quote from the draft standard is pretty
clear, isn't it? I do not have a copy of the final version, but I would not
imagine that it changed substantially.

It's not topical on any other group, either, by the same logic.

Well, that's too bad then. How about splittig the question and asking in a
Linux forum about Linux, in a Windows forum about Windows, in a gcc forum
about gcc, ...?


Best

Kai-Uwe Bux
 
I

Ian Collins

Phlip said:
Default User wrote:

"The clock function tells how much time the calling process has used."

It is not the wall time, it's the process time.


Despite, uh, saying "wall" time at the top of the page?

Please Google [wall-clock time]. If it's meaning isn't related to the
movement of a clock's hands while the program runs, then I'm wrong.
Otherwise, the MSDN page is inconsistent with itself.

A Win32 test involving Sleep() and two clock() calls would instantly answer
questions about the actual implementation.

And please be more careful with your own claims.
Brian's claim is correct.

From C99 7.23.2.1:

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.
 
M

Matt

Phlip said:
Matt wrote:

Does clock_t clock() measure real time or process time? Unless I have
missed something, Stroustrup's treatment in TC++PL (section D.4.4.1) is
none too clear on that question.

Is clock() well-implemented for the major compilers g++, VC++, and
Macintosh compilers and other C++ compilers on Linux/Unix, Windows, and
MacOS?


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_clock.asp

Note the compatibility is "ANSI", which is MS's code for the "ISO C
Standard". All compilers worth using will have it.

Then note the first line is "Calculates the wall-clock time used by the
calling process." That "wall-clock" is jargon for real time, not process
time. And "used by the calling process" means the "epoch" starts when the
program starts.

In future, please hit a question with Google before posting it! I answered
all your questions with the first citation for [msdn clock_t].

Thanks for your answers to all my two questions!

Your answer to my first question was wrong.

Your answer to my second question was nonexistent.
 
V

Victor Bazarov

Matt said:
Phlip said:
Matt wrote:

Does clock_t clock() measure real time or process time? Unless I
have missed something, Stroustrup's treatment in TC++PL (section
D.4.4.1) is none too clear on that question.

Is clock() well-implemented for the major compilers g++, VC++, and
Macintosh compilers and other C++ compilers on Linux/Unix, Windows,
and MacOS?


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_clock.asp

Note the compatibility is "ANSI", which is MS's code for the "ISO C
Standard". All compilers worth using will have it.

Then note the first line is "Calculates the wall-clock time used by
the calling process." That "wall-clock" is jargon for real time, not
process time. And "used by the calling process" means the "epoch"
starts when the program starts.

In future, please hit a question with Google before posting it! I
answered all your questions with the first citation for [msdn
clock_t].

Thanks for your answers to all my two questions!

Your answer to my first question was wrong.

In what way was it "wrong"? You asked "real time or process time"? The
answer was 'wall-clock' and the explanation given that it is "real time
not process time". How in hell is it "wrong"?
Your answer to my second question was nonexistent.

The asnwer to your second question was "All complers worth using will have
it". To me it means "implemented". As to "well" or "not well", we cannot
tell -- it's implementation-specific and we don't discuss implementations.
Most compilers have their own newsgroups, consider posting there. Also,
if you need information on "other compilers for <blah> platform", please
consider asking in the newsgroup dedicated to the platform.

Also, complaining about free advice is non-sensical. You asked a question,
you received _a_ response. Take it or leave it, don't bitch about it.

V
 
P

peter koch

Matt said:
Does clock_t clock() measure real time or process time?
[snip]

To add to the confusion, I have just tested a program using clock() on
Solaris and Windows NT. On both platforms clock() did seem to give
wall-clock (that is elapsed) time - the same result as is available
with gettickcount() on windows and time() on Solaris.
To get processor time, I had to use GetProcessInfo on Windows and
getrusage on Solaris (not 100% confident on those last names - i do not
have the code present).

/Peter
 
P

Phlip

peter said:
Matt wrote:
To add to the confusion, I have just tested a program using clock() on
Solaris and Windows NT. On both platforms clock() did seem to give
wall-clock (that is elapsed) time - the same result as is available
with gettickcount() on windows and time() on Solaris.
To get processor time, I had to use GetProcessInfo on Windows and
getrusage on Solaris (not 100% confident on those last names - i do not
have the code present).

Did your test use a Sleep(big number)? Sleep's documentation claims it
suspends the CPU activity for a process...
 
P

peter koch

Phlip said:
Did your test use a Sleep(big number)? Sleep's documentation claims it
suspends the CPU activity for a process...

Yes. I did measure a few busy loops (calculating e.g. sin() repeatedly)
and one sleep for app. six seconds. The results were consistent: clock
gave same result as gettickcount on windows, whereas GetProcessInfo
gave the same results on the busy loops and 0 when calling Sleep.

/Peter
 
P

Phlip

peter said:
Yes. I did measure a few busy loops (calculating e.g. sin() repeatedly)
and one sleep for app. six seconds. The results were consistent: clock
gave same result as gettickcount on windows, whereas GetProcessInfo
gave the same results on the busy loops and 0 when calling Sleep.

Thanks. You have restored my faith in MSDN.
 
P

peter koch

Phlip said:
Thanks. You have restored my faith in MSDN.
From the other posts it seems to me that Windows NT as well as Solaris
got it wrong. This also explains my collegues surprise when I told them
that clock() returns elapsed time - they thought that clock() returned
processortime.
Could it be that Solaris and Windows NT shares a flawed codebase from
some time in the distant past?

/Peter
 
D

Default User

Phlip said:
Thanks. You have restored my faith in MSDN.

I'm not sure what faith. The documentation is either inconsistent,
confusing, or just wrong.

This whole conversation seemed somewhat familiar, a bit of Groupling
came up with this:

<http://groups.google.com/group/comp.lang.c++/browse_frm/thread/fb0f9076
19ac71fe/7a52324dcae9a6fd>


Victor said then that Microsoft screwed up the implementation of
clock() in Visual Studio, and my little experiment seemed to confirm
that.



Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top