Any idea why clock() returns 0 or 10000

Discussion in 'C++' started by none, Jul 10, 2007.

  1. none

    none Guest

    Hello,
    can you help?
    if I include:
    #include <time.h>

    and run the code:
    clock_t t = clock();
    cout << "t is " << t << endl;

    I get (seemingly randomly) "t is 0" or "t is 10000"

    Any ideas?
    thank you.

    I am using:
    gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) via Eclipse.

    Thanks you in advance.

    John
    none, Jul 10, 2007
    #1
    1. Advertising

  2. none

    Jim Langston Guest

    "none" <""john\"@(none)"> wrote in message
    news:4693690c$0$15858$...
    > Hello,
    > can you help?
    > if I include:
    > #include <time.h>
    >
    > and run the code:
    > clock_t t = clock();
    > cout << "t is " << t << endl;
    >
    > I get (seemingly randomly) "t is 0" or "t is 10000"
    >
    > Any ideas?
    > thank you.
    >
    > I am using:
    > gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) via Eclipse.


    Apparently you clock has a detail of 10,000ns (I think it's nano seconds?).
    clock() says how many clock ticks have happended since your program started.
    since it's a short program, it usually hasn't hit 10000 by the time clock_tt
    = clock(); is reached, hence you get 0. Every now and then, maybe cause of
    other background processes, it takes a little longer so you see the tick
    over.

    Try something like:

    for ( int i = 0; i < 100; ++i )
    cout << "clock is " << clock() << "\n";

    and you should see it change
    Jim Langston, Jul 10, 2007
    #2
    1. Advertising

  3. none

    none Guest

    Jim Langston wrote:
    > "none" <""john\"@(none)"> wrote in message
    > news:4693690c$0$15858$...
    >> Hello,
    >> can you help?
    >> if I include:
    >> #include <time.h>
    >>
    >> and run the code:
    >> clock_t t = clock();
    >> cout << "t is " << t << endl;
    >>
    >> I get (seemingly randomly) "t is 0" or "t is 10000"
    >>
    >> Any ideas?
    >> thank you.
    >>
    >> I am using:
    >> gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) via Eclipse.

    >
    > Apparently you clock has a detail of 10,000ns (I think it's nano seconds?).
    > clock() says how many clock ticks have happended since your program started.
    > since it's a short program, it usually hasn't hit 10000 by the time clock_tt
    > = clock(); is reached, hence you get 0. Every now and then, maybe cause of
    > other background processes, it takes a little longer so you see the tick
    > over.
    >
    > Try something like:
    >
    > for ( int i = 0; i < 100; ++i )
    > cout << "clock is " << clock() << "\n";
    >
    > and you should see it change
    >
    >
    >

    Thanks for the reply Jim,
    Unfortunately it results in 100 x "clock is 0", or 100 x "clock is 10000"

    Interesting.
    none, Jul 10, 2007
    #3
  4. none

    anon Guest

    none wrote:
    > Jim Langston wrote:
    >> "none" <""john\"@(none)"> wrote in message
    >> news:4693690c$0$15858$...
    >>> Hello,
    >>> can you help?
    >>> if I include:
    >>> #include <time.h>
    >>>
    >>> and run the code:
    >>> clock_t t = clock();
    >>> cout << "t is " << t << endl;
    >>>
    >>> I get (seemingly randomly) "t is 0" or "t is 10000"
    >>>
    >>> Any ideas?
    >>> thank you.
    >>>
    >>> I am using:
    >>> gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) via Eclipse.

    >>
    >> Apparently you clock has a detail of 10,000ns (I think it's nano
    >> seconds?). clock() says how many clock ticks have happended since your
    >> program started. since it's a short program, it usually hasn't hit
    >> 10000 by the time clock_tt = clock(); is reached, hence you get 0.
    >> Every now and then, maybe cause of other background processes, it
    >> takes a little longer so you see the tick over.
    >>
    >> Try something like:
    >>
    >> for ( int i = 0; i < 100; ++i )
    >> cout << "clock is " << clock() << "\n";
    >>
    >> and you should see it change
    >>
    >>
    >>

    > Thanks for the reply Jim,
    > Unfortunately it results in 100 x "clock is 0", or 100 x "clock is 10000"
    >
    > Interesting.


    http://linux.die.net/man/3/clock

    Try this:

    for ( long i = 0; i < 100000000; ++i )
    cout << "clock is " << clock() << "\n";

    and I am sure you will see it changing
    anon, Jul 10, 2007
    #4
  5. none

    none Guest

    Thanks very much anon and Jim - you're right it does change.


    >
    > Try this:
    >
    > for ( long i = 0; i < 100000000; ++i )
    > cout << "clock is " << clock() << "\n";
    >
    > and I am sure you will see it changing
    none, Jul 10, 2007
    #5
  6. none

    James Kanze Guest

    On Jul 10, 1:27 pm, "Jim Langston" <> wrote:
    > "none" <""john\"@(none)"> wrote in message
    > news:4693690c$0$15858$...
    > > can you help?
    > > if I include:
    > > #include <time.h>


    > > and run the code:
    > > clock_t t = clock();
    > > cout << "t is " << t << endl;


    > > I get (seemingly randomly) "t is 0" or "t is 10000"


    > > Any ideas?


    > > I am using:
    > > gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) via Eclipse.


    Knowing the OS would help as well. clock() usually resolves to
    a system function.

    > Apparently you clock has a detail of 10,000ns (I think it's nano seconds?).


    It depends on the system. And there are two separate issues:
    units (i.e. 1/CLOCKS_PER_SEC) and resolution. The Posix
    standard requires that CLOCKS_PER_SEC be 1000000, so the unit
    will always be a microsecond. The resolution varies, but I
    suspect that it's rarely this much.

    The value you get on the first call to clock isn't specified,
    either (although it shouldn't represent much more than the time
    the program has already run), as does what clock() actually
    measures.

    > clock() says how many clock ticks have happended since your
    > program started. since it's a short program, it usually
    > hasn't hit 10000 by the time clock_tt = clock(); is reached,
    > hence you get 0. Every now and then, maybe cause of other
    > background processes, it takes a little longer so you see the
    > tick over.


    The actual clock ticks come at specific intervals, independantly
    of when the program actually started. The actual clock ticks
    are probably of much greater granularity than 1 microsecond, or
    whatever. If the first clock tick happens to fall between the
    start of his program and his call to clock(), he could get the
    granularity. (Under Solaris, the first call to clock() always
    returns 0, but this isn't true for other systems.)

    Note too that clock() returns "the implementation\u2019s best
    approximation to the processor time used by the program". A
    system in which clock() increments due to activity by other
    programs is broken.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jul 11, 2007
    #6
    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. qazmlp
    Replies:
    5
    Views:
    709
    Chris Smith
    Feb 19, 2004
  2. Jerry

    Thread.sleep(10000)

    Jerry, Aug 1, 2005, in forum: Java
    Replies:
    2
    Views:
    15,140
    John Currier
    Aug 2, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,804
    Smokey Grindel
    Dec 2, 2006
  4. Dr Mephesto

    App idea, Any idea on implementation?

    Dr Mephesto, Feb 4, 2008, in forum: Python
    Replies:
    3
    Views:
    710
    Dennis Lee Bieber
    Feb 5, 2008
  5. Replies:
    0
    Views:
    630
Loading...

Share This Page