Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Classify clock() for variables of type clock_t

Reply
Thread Tools

Classify clock() for variables of type clock_t

 
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      07-12-2013
> On Friday, July 12, 2013 10:48:53 AM UTC-4, Drew Lawson wrote:
> 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.
 
Reply With Quote
 
 
 
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      07-12-2013
> On Friday, July 12, 2013 10:48:53 AM UTC-4, Drew Lawson wrote:
> 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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-12-2013
On 7/12/2013 11:05 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>> On Friday, July 12, 2013 10:48:53 AM UTC-4, Drew Lawson wrote:
>> 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.


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
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      07-12-2013
> On Friday, July 12, 2013 10:48:53 AM UTC-4, Drew Lawson wrote:
> 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.
 
Reply With Quote
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      07-12-2013
> On Friday, July 12, 2013 11:46:56 AM UTC-4, Victor Bazarov wrote:
> 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,
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      07-12-2013

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>> On Friday, July 12, 2013 10:52:26 AM UTC-4, Paavo Helde wrote:
>> 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).


Your friends should have said you *should* not use clock.


 
Reply With Quote
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      07-12-2013
> On Friday, July 12, 2013 11:46:56 AM UTC-4, Victor Bazarov wrote:
> 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.


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-13-2013
On Friday, July 12, 2013 3:15:55 PM UTC+1, Paavo Helde wrote:
> James Kanze <(E-Mail Removed)> wrote in


[...]
> > 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".

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


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

--
James
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      07-19-2013
On Thu, 2013-07-18, Juha Nieminen wrote:
> (E-Mail Removed) wrote:
>> I was recently notified that I can not use "clock()" even though it
>> works fairly well on my computer!

>
> Who's stopping you?


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

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      07-19-2013
On Friday, 19 July 2013 10:14:16 UTC+3, Jorgen Grahn wrote:
> On Thu, 2013-07-18, Juha Nieminen wrote:
> > (E-Mail Removed) wrote:
> >> I was recently notified that I can not use "clock()" even though it
> >> works fairly well on my computer!

> >
> > Who's stopping you?

>
> 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".
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: "Why is it necessary to classify data according to its type incomputer programming?" tm C Programming 7 07-11-2010 10:10 PM
Re: How to classify RDP traffic? Mr. Nobody Cisco 0 07-12-2008 04:00 PM
Re: QoS: How to classify RDP traffic? Bod43@hotmail.co.uk Cisco 0 07-12-2008 10:08 AM
clock_t clock() ambiguity Matt C++ 30 08-01-2006 11:16 PM
how to print clock_t variables? ykz C Programming 4 11-03-2003 04:53 PM



Advertisments