Classify clock() for variables of type clock_t

Discussion in 'C++' started by clusardi2k@aol.com, Jul 12, 2013.

  1. Guest

    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,
     
    , Jul 12, 2013
    #1
    1. Advertising

  2. On 7/12/2013 9:12 AM, wrote:
    > 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
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 12, 2013
    #2
    1. Advertising

  3. James Kanze Guest

    On Friday, July 12, 2013 2:41:03 PM UTC+1, Paavo Helde wrote:
    > wrote in
    > news::


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


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

    --
    James
     
    James Kanze, Jul 12, 2013
    #3
  4. On 7/12/2013 9:48 AM, James Kanze wrote:
    > On Friday, July 12, 2013 2:41:03 PM UTC+1, Paavo Helde wrote:
    >> wrote in
    >> news::

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

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


    It doesn't? 8-O

    Care to elaborate?

    > [..]


    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 12, 2013
    #4
  5. Guest

    On Friday, July 12, 2013 9:48:57 AM UTC-4, James Kanze wrote:
    > 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,
     
    , Jul 12, 2013
    #5
  6. Guest

    > On Friday, July 12, 2013 10:30:11 AM UTC-4, Paavo Helde wrote:

    > 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.
     
    , Jul 12, 2013
    #6
  7. Guest

    > 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.
     
    , Jul 12, 2013
    #7
  8. Guest

    > 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).
     
    , Jul 12, 2013
    #8
  9. Guest

    > 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.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.
     
    , Jul 12, 2013
    #9
  10. Guest

    > 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 ( 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.
     
    , Jul 12, 2013
    #10
  11. Guest

    > 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.
     
    , Jul 12, 2013
    #11
  12. Guest

    > 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.
     
    , Jul 12, 2013
    #12
  13. On 7/12/2013 11:05 AM, 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
     
    Victor Bazarov, Jul 12, 2013
    #13
  14. Guest

    > 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.
     
    , Jul 12, 2013
    #14
  15. Guest

    > 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,
     
    , Jul 12, 2013
    #15
  16. osmium Guest

    <> wrote in message
    news:...
    >> 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.
     
    osmium, Jul 12, 2013
    #16
  17. Guest

    > 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.
     
    , Jul 12, 2013
    #17
  18. James Kanze Guest

    On Friday, July 12, 2013 3:15:55 PM UTC+1, Paavo Helde wrote:
    > James Kanze <> 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
     
    James Kanze, Jul 13, 2013
    #18
  19. Jorgen Grahn Guest

    On Thu, 2013-07-18, Juha Nieminen wrote:
    > 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 .
     
    Jorgen Grahn, Jul 19, 2013
    #19
  20. Öö Tiib Guest

    On Friday, 19 July 2013 10:14:16 UTC+3, Jorgen Grahn wrote:
    > On Thu, 2013-07-18, Juha Nieminen wrote:
    > > 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".
     
    Öö Tiib, Jul 19, 2013
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ykz

    how to print clock_t variables?

    ykz, Oct 31, 2003, in forum: C Programming
    Replies:
    4
    Views:
    7,124
    Dan Pop
    Nov 3, 2003
  2. Matt
    Replies:
    30
    Views:
    2,689
    Default User
    Aug 2, 2006
  3. palewire
    Replies:
    1
    Views:
    228
    Paul Rubin
    Jul 7, 2009
  4. tm
    Replies:
    7
    Views:
    450
  5. Don French

    active_support classify truncating

    Don French, Jun 29, 2010, in forum: Ruby
    Replies:
    1
    Views:
    112
    Don French
    Jun 29, 2010
Loading...

Share This Page