Classify clock() for variables of type clock_t

C

clusardi2k

Hello,

I was recently notified that I can not use "clock()" even though it works fairly well on my computer!

Question: Based on this information, how can I identify other functions that I am not allowed to use?

Thanks,
 
V

Victor Bazarov

I was recently notified that I can not use "clock()" even though it
works fairly well on my computer!

"Notified"? By whom or by what?
Question: Based on this information, how can I identify other
functions that I am not allowed to use?

I suppose the only way to "identify other functions" is to tap the same
source of information. Whoever "notified" you that you "can not use"
function 'clock' should be able to supply you with the list of other
functions you "can not use" (whatever that means in your circumstances).

V
 
J

James Kanze

(e-mail address removed) wrote in
It might help if there were any rationale why you cannot use clock(). For
example, if your evil overlord does not like words beginning with c, then
you cannot use chdir(), copy_backward() etc.
As I understand you have absolutely no idea why clock() was banned ;-) In
this case I'm afraid we can't help you either.

The only reason I can think of is that it doesn't work in VC++.
Otherwise: I can't think of any reason you would want to use it
in released software anyway. In fact, about the only place
I would expect to see it is in a benchmarking harness. (One
might insert it temporarily for some quick timings in situ in
the application, but in that case, you'd certainly remove it
before delivery.)
 
C

clusardi2k

On Friday, July 12, 2013 2:41:03 PM UTC+1, Paavo Helde wrote:
It might help if there were any rationale why you cannot use clock(). For>
example, if your evil overlord does not like words beginning with c, then>
you cannot use chdir(), copy_backward() etc. > As I understand you have
absolutely no idea why clock() was banned ;-) In > this case I'm afraid we
can't help you either. The only reason I can think of is that it doesn't work
in VC++. Otherwise: I can't think of any reason you would want to use it in
released software anyway. In fact, about the only place I would expect tosee
it is in a benchmarking harness. (One might insert it temporarily for some
quick timings in situ in the application, but in that case, you'd certainly
remove it before delivery.) -- James

One reason that I was told was that it's based on program execution time and not system time. Someone, who has a lot of programming experience, latertold me "clock()" won't work on all computers. I think the second person was trying to make me feel better and was too busy to give me better reasoning.

The reason the problem came up was I noticed that using "clock ()" would generate larger and larger errors each time a routine was called. After the error reached nearly one second, the error would drop down to just above zero and repeat its building up. My feelings were that during this one second period my code would increasonly do other things. So, at the start of the one second my code would do a little, but near the end of the one second it would be doing a lot more.

What do you think? Should I used variables of type "SYSTEMTIME" instead. Doing math with "SYSTEMTIME" variables is harder etc.

Thanks,
 
C

clusa

And what do you mean by "clock() generates errors"? Do you mean it returns an
error code (clock_t(-1))?

Sorry, I meant to say that when a routine containing "clock()" gets called the clock count becomes later and later. Basically, I was expecting my routine to be called at a specific time, but during one second intervals the routine was getting called later and later. So, the amount of time between the expected call time and the actual arrival of the call I was calling an error.

"clock()" was not returning an error code.
 
C

clusardi2k

Perhaps things would be clearer if you stated what you are trying to use clock
() for.

I did not write the original code. It has always worked fine. I'm just adding code to it. When I try to add "clock()" to the code, it's the very first change that I've made to it.

So, I use "clock()" for this:

(1) I do the following in a routine, call it r1.
(2) When "clock()" returns a value greater or equal to t2 (see t2 below), I do the following:
(2) I first call "clock()" and save it to a variable, call it t1.
(3) I add a time span (of 30 seconds) to t1, (call the time span ts1). The resulting addition is saved to a variable, call it t2. The code looks "something" like this:
t2 = t1 + ((float)CLOCKS_PER_SEC * 30.3).
(4) The code goes off and does other things, but eventually arrives at step (1) again.
 
C

clusardi2k

And so you came to the conclusion that the problem must be with clock() as your
code is certainly correct? Hilarious! ;-) p

No, I want to use "clock()", but two different people told me that I cannot (see above).
 
C

clusardi2k

Perhaps things would be clearer if you stated what you are trying to use clock
() for.

I did not write the original code. It has always worked fine. I'm just adding code to it. When I try to add "clock()" to the code, it's the very first change that I've made to it.

So, I use "clock()" for this:

(1) I do the following in a routine, call it r1.
(2) When "clock()" returns a value greater or equal to t2 (see t2 below), I do the following:
(2) I first call "clock()" and save it to a variable, call it t1.
(3) I add a time span (of 30.3 seconds) to t1, (call the time span ts1). The resulting addition is saved to a variable, call it t2. The code looks "something" like this:
t2 = t1 + ((float)CLOCKS_PER_SEC * 30.3).
(4) The code goes off and does other things, but eventually arrives at step (1) again.
 
C

clusardi2k

Perhaps things would be clearer if you stated what you are trying to use clock
() for.

I did not write the original code. It has always worked fine. I'm just adding code to it. When I try to add "clock()" to the code, it's the very firstchange that I've made to it.

So, I use "clock()" for this:

(1) I do the following in a routine, call it r1.
(2) When "clock()" returns a value greater or equal to t2 (see t2 below), Ido the following:
(3) I first call "clock()" and save it to a variable, call it t1.
(4) I add a time span (of 30.3 seconds) to t1, (call the time span ts1). The resulting addition is saved to a variable, call it t2.
(5) I computer a lateness variable, call it late1. late1 is a computation of the actual arrival time minus expected time of arrival.

Via from memory, the code looks "something" like this:

#include time.h

clock_t t1, t2, ts1;

ts1 = (float)CLOCKS_PER_SEC * 30.3;

t1 = clock ();

if ( t3 >= t2 )
{
late1 = t3 - t2;
t2 = t1 + ts1 + late1;
}

(6) The code goes off and does other things, but eventually arrives at step(1) again.

Note: Obviously, late1 is not absolutely accurate because each instruction adds time, but the above code (in the "if") is almost exactly as I coded it.. So, late1 repeatedly and slowly increases from small values near zero to about one second. late1 is what I earlier called error.
 
C

clusardi2k

Perhaps things would be clearer if you stated what you are trying to use clock
() for.

I did not write the original code. It has always worked fine. I'm just adding code to it. When I try to add "clock()" to the code, it's the very firstchange that I've made to it.

So, I use "clock()" for this:

(1) I do the following in a routine, call it r1.
(2) When "clock()" returns a value greater or equal to t2 (see t2 below), Ido the following:
(3) I first call "clock()" and save it to a variable, call it t1.
(4) I add a time span (of 30.3 seconds) to t1, (call the time span ts1). The resulting addition is saved to a variable, call it t2.
(5) I computer a lateness variable, call it late1. late1 is a computation of the actual arrival time minus expected time of arrival.

Via from memory, the code looks "something" like this:

#include time.h

clock_t t1, t2, ts1;

ts1 = (float)CLOCKS_PER_SEC * 30.3;

t1 = clock ();

if ( t1 >= t2 )
{
late1 = t1 - t2;
t2 = t1 - late1;
}

(6) The code goes off and does other things, but eventually arrives at step(1) again.

Note: Obviously, late1 is not absolutely accurate because each instruction adds time, but the above code (in the "if") is almost exactly as I coded it.. So, late1 repeatedly and slowly increases from small values near zero to about one second. late1 is what I earlier called error.
 
C

clusardi2k

Perhaps things would be clearer if you stated what you are trying to use clock
() for.


I did not write the original code. It has always worked fine. I'm just adding code to it. When I try to add "clock()" to the code, it's the very firstchange that I've made to it.

So, I use "clock()" for this:

(1) I do the following in a routine, call it r1.
(2) When "clock()" returns a value greater or equal to t2 (see t2 below), Ido the following:
(3) I first call "clock()" and save it to a variable, call it t1.
(4) I add a time span (of 30.3 seconds) to t1, (call the time span ts1). The resulting addition is saved to a variable, call it t2.
(5) I computer a lateness variable, call it late1. late1 is a computation of the actual arrival time minus expected time of arrival.

Via from memory, the code looks "something" like this:

#include time.h

clock_t t1, t2, ts1;

ts1 = (float)CLOCKS_PER_SEC * 30.3;

t1 = clock ();

if ( t1 >= t2 )
{
late1 = t1 - t2;
t2 = t1 + ts1 - late1;
}

(6) The code goes off and does other things, but eventually arrives at step(1) again.

Note: Obviously, late1 is not absolutely accurate because each instruction adds time, but the above code (in the "if") is almost exactly as I coded it.. So, late1 repeatedly and slowly increases from small values near zero to about one second. late1 is what I earlier called error.
 
V

Victor Bazarov

I did not write the original code. It has always worked fine. I'm just adding code to it. When I try to add "clock()" to the code, it's the very first change that I've made to it.

So, I use "clock()" for this:

(1) I do the following in a routine, call it r1.
(2) When "clock()" returns a value greater or equal to t2 (see t2 below), I do the following:
(2) I first call "clock()" and save it to a variable, call it t1.
(3) I add a time span (of 30 seconds) to t1, (call the time span ts1). The resulting addition is saved to a variable, call it t2. The code looks "something" like this:
t2 = t1 + ((float)CLOCKS_PER_SEC * 30.3).
(4) The code goes off and does other things, but eventually arrives at step (1) again.

What you describe seems an attempt to implement some kind of scheduler
that performs some actions every so often (30.3 seconds in your case).
I think you should explore the standard facilities that allow a thread
to be suspended (sleep) either _for_ a particular time span or _until_ a
particular moment in [system] time.

V
 
C

clusardi2k

Perhaps things would be clearer if you stated what you are trying to use clock
() for.



I did not write the original code. It has always worked fine. I'm just adding code to it. When I try to add "clock()" to the code, it's the very firstchange that I've made to it.

So, I use "clock()" for this:

(1) I do the following in a routine, call it r1.
(2) I first call "clock()" and save it to a variable, call it t1.
(3) When "clock()" returns a value greater or equal to t2 (see t2 below), Ido the following:
(4) I add a time span (of 30.3 seconds) to t1, (call the time span ts1). The resulting addition is saved to a variable, call it t2.
(5) I computer a lateness variable, call it late1. late1 is a computation of the actual arrival time minus expected time of arrival.

Via from memory, the code looks "something" like this:

#include time.h

clock_t t1, t2, ts1;

ts1 = (float)CLOCKS_PER_SEC * 30.3;

void r1 () {
t1 = clock ();

if ( t1 >= t2 )
{
late1 = t1 - t2;
t2 = t1 + ts1 - late1;
}
}

(6) The code goes off and does other things, but eventually arrives at step(1) again.

Note: Obviously, late1 is not absolutely accurate because each instruction adds time, but the above code (in the "if") is almost exactly as I coded it.. So, late1 repeatedly and slowly increases from small values near zero to about one second. late1 is what I earlier called error.
 
C

clusardi2k

What you describe seems an attempt to implement some kind of scheduler that
performs some actions every so often (30.3 seconds in your case). I think you
should explore the standard facilities that allow a thread to be suspended
(sleep) either _for_ a particular time span or _until_ a particular moment in
[system] time.

Ok, can someone be kind enough to elaborate of this with some good, useful, executable code!

Thanks,
 
C

clusardi2k

I think you should explore the standard facilities that allow a thread to be
suspended (sleep) either _for_ a particular time span or _until_ a particular
moment in [system] time.

I can only use Visual Studio 2010.
 
J

James Kanze

[...]
The only reason I can think of is that it doesn't work in VC++.
It does work in MSVC++, although doing slightly different thing than in
Linux (measuring wall time instead of process cpu time). However, it
seems its behavior varies in different *nix flavors.

The meaning of clock is specified by the C standard: "The
clock() function shall return the implementation's best
approximation to the processor time used by the process since
the beginning of an implementation-defined era related only to
the process invocation." Under Windows, this means that the
return value should be based on the lpUserTime field returned by
GetProcessTimes.

And the only real differences I've encountered in different Unix
variants is the value of CLOCKS_PER_SECOND (and then, only in
legacy Unices---modern Unix requires it to be one million) and
the value returned by the first call to clock().
Maybe the behavior differencies were the reason of the ban? In this case
there are many other functions behaving slightly differently on different
platforms. Printf() rounding rules pop into mind first (not sure if
iostream rounding is also affected or not).

That's basically what I said: the Windows implementation is
broken, and doesn't have standard conformant semantics. It's
not a "slight difference".
Yes, benchmarking is certainly the main use case for clock(). When
benchmarking one should take care anyway that there are no other
significant programs running at the time of tests, and the benchmarked
sections of code typically do not contain any sleeps, so the difference
between Windows/Linux is not so important any more.

On the usual desktop platforms, you cannot prevent other
processes from running. And my experience is that the values
returned under Solaris or Linux are usable; those under Windows,
usually not. (But in all cases, I'm running without admin
rights, and cannot stop background processes; e.g. the
anti-virus kicks in when it wants to, and I cannot turn it off,
even for a second. If you're running on a single user system,
with only a single core, with all services turned off, and no
connection to any network, you might be right.)
 
Ö

Öö Tiib

People predictably already asked that question ~1 week ago. (I'm not
sure I understood the answer.)

I understood that some more experienced developers from somewhere (team,
client). It seemed that OP left the exact relations dim for purpose and preferred "other people".
 

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

Forum statistics

Threads
474,262
Messages
2,571,050
Members
48,769
Latest member
Clifft

Latest Threads

Top