473,698 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

clock_t clock() ambiguity

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?
Jul 29 '06 #1
30 17303
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/de..._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].

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jul 29 '06 #2
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.
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

Jul 29 '06 #3
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.
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
Jul 29 '06 #4
Kai-Uwe Bux wrote:
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??
>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.
It's not topical on any other group, either, by the same logic.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jul 29 '06 #5
Phlip wrote:
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/de...ry/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
Jul 29 '06 #6
mlimber wrote:
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.

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

Jul 29 '06 #7
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.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jul 29 '06 #8
Phlip wrote:
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?
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
Jul 29 '06 #9
Phlip wrote:
Kai-Uwe Bux wrote:
>3 The clock function returns the implementation? s best approximation to
the
processor time used by the program since the beginning of an
implementati on-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.
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.

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

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

Jul 29 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6693
by: James Harriman | last post by:
Hi, I need to be able to measure a time interval in milliseconds on a windows machine. I have tried using time.clock() but it appears to measure time in seconds...Is there a way to measure time more precisely? Regards, James Harriman v38zy@unb.ca
0
408
by: Peger, Daniel H. | last post by:
Hi, I'm having a slight problem with the precision of the standard c++ clock function as it is defined in time.h . On my system the shortest meassurable time period is 0.01 seconds, but for my programm I need to meassure the processing time in ms. Is there perhaps something like an environment variable controlling the precision of clock(), or is there any other way of meassuring time in ANSI C in the ms domain??
33
47611
by: Pushkar Pradhan | last post by:
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();
54
3993
by: CoreyWhite | last post by:
The following experiment is a demonstration of TIME TRAVEL. When writing this program, and testing it out I found that sometimes the program would engage itself in time travel but other times it would not. After careful testing and reprogramming I have optimized the code so it should work every time, however I recommend that you leave the room while it is running and think of something else. It takes about 5 minuets to run so it is...
12
4093
by: cenktarhancenk | last post by:
is there a way to display a ticking clock in a web page using javascript? but not in a textbox, rather as text that i can change the style, font etc. cenk tarhan
0
9164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8898
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7734
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6524
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3051
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.