change clock's precision

Discussion in 'C++' started by Robert, Oct 5, 2005.

  1. Robert

    Robert Guest

    Hello all,

    I use clock() to measure the time cost in my program.
    But the t_clock always be the multiply of 10, for example 10, 20, 30,
    40,...
    And I want to get the precision of 1, such as 11, 12, 13, 14...
    How can I change the t_clock's precision?

    Code list below:
    //-----------------
    #include <cstdio>
    #include <fstream.h>
    #include <ctime>

    void main() {
    int x;
    clock_t t_click,t_start;

    t_start = clock();
    for (x=0;x<60;x++) {
    printf("%d\n",x);
    }

    t_click = clock() - t_start;
    printf("Time Elapsed %d\n",t_click);

    }
    //-------------------
    BTW, I use VC6.0. The environment is P3 600MHz and WinXP.

    Any suggestions will be appreciated!

    Best regards,
    Davy
    Robert, Oct 5, 2005
    #1
    1. Advertising

  2. Robert wrote:
    >
    > Hello all,
    >
    > I use clock() to measure the time cost in my program.
    > But the t_clock always be the multiply of 10, for example 10, 20, 30,
    > 40,...
    > And I want to get the precision of 1, such as 11, 12, 13, 14...
    > How can I change the t_clock's precision?


    You can't.
    The 'precision' is given to you and there is no way to change
    that. That may eg. be because the hardware doesn't support a
    finer granularity.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Oct 5, 2005
    #2
    1. Advertising

  3. In article <>,
    Robert <> wrote:
    >I use clock() to measure the time cost in my program.
    >But the t_clock always be the multiply of 10, for example 10, 20, 30,
    >40,...
    >And I want to get the precision of 1, such as 11, 12, 13, 14...
    >How can I change the t_clock's precision?


    There is no way to do that in standard C. Keep in mind that
    clock_t is a count of CLOCKS_PER_SEC and not an absolute time,
    so a value of 10 might mean 10 femtoseconds with the extra digit
    reserved for the next board rev with the built-in 1 femtosecond
    counter.


    >Code list below:
    >//-----------------


    // comments are not allowed in C89.

    >#include <cstdio>
    >#include <fstream.h>
    >#include <ctime>


    Those are not standard C headers. If you expect us to be able to
    understand your program in comp.lang.c, you will have to provide the
    expansion of the headers.


    >void main() {


    In standards-compliant C code, main() must return an int.

    Also, if you are not declaring parameters for main, it is
    better practice to prototype it as main(void)

    > int x;
    > clock_t t_click,t_start;
    >
    > t_start = clock();
    > for (x=0;x<60;x++) {
    > printf("%d\n",x);
    > }
    >
    > t_click = clock() - t_start;
    > printf("Time Elapsed %d\n",t_click);


    You did not divide by CLOCKS_PER_SEC so printing out the number
    of ticks gives us no idea how long the program took.

    >}


    In C89, if you do not return a value from main, the exit status
    of the program is undefined.

    >//-------------------
    >BTW, I use VC6.0. The environment is P3 600MHz and WinXP.


    >Any suggestions will be appreciated!


    Any higher precision timing would be implementation specific,
    and is thus not a suitable topic for comp.lang.c .
    This topic is, by the way, disposed of in the comp.lang.c FAQ...
    --
    Programming is what happens while you're busy making other plans.
    Walter Roberson, Oct 5, 2005
    #3
  4. "Robert" <> wrote in message
    news:...

    > I use clock() to measure the time cost in my program.
    > But the t_clock always be the multiply of 10, for example 10, 20, 30,
    > 40,...
    > And I want to get the precision of 1, such as 11, 12, 13, 14...
    > How can I change the t_clock's precision?


    You cannot change the precision; you can only measure. Windows allocates
    each thread a certain amount of time slice. Apparently the granularity of
    that slice on your system is 10/CLOCKS_PER_SEC seconds.

    Use a different timing function.

    [It is completely off-topic on this group, and my Windows programming skills
    are very rusty, but you may start reading on GetTickCount and
    QueryPerformanceCounter functions.]

    > Code list below:
    > //-----------------
    > #include <cstdio>
    > #include <fstream.h>


    <fstream.h> is pre-standard; use <fstream> instead. (Though you don't use it
    in the program.)

    > #include <ctime>
    >
    > void main() {


    'void main' is not standard; should be 'int main'

    > int x;
    > clock_t t_click,t_start;
    >
    > t_start = clock();
    > for (x=0;x<60;x++) {
    > printf("%d\n",x);
    > }
    >
    > t_click = clock() - t_start;
    > printf("Time Elapsed %d\n",t_click);


    This is one example that demonstrates the superiority of C++'s operator<<
    overloading over printf. Since we don't know the actual type of clock_t, we
    cannot know what format identifier to use with printf (%d didn't match
    clock_t on my system).

    This works:

    cout << "Time Elapsed: " << t_click << '\n';

    > }
    > //-------------------
    > BTW, I use VC6.0.


    In that case, in addition to using 'int main', you will have to explicitly
    add a return statement at the end of your main function:

    return 0;

    That statement is not necessary and most other compilers get that right; but
    VC++60 needs you to type it. In other words, even though the following is a
    valid C++ program, VC++60 will need you to add a return statement.

    int main()
    {}

    Ali
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Oct 5, 2005
    #4
  5. Robert

    Mark B Guest

    "Robert" <> wrote in message
    news:...
    > Hello all,
    >
    > I use clock() to measure the time cost in my program.
    > But the t_clock always be the multiply of 10, for example 10, 20, 30,
    > 40,...
    > And I want to get the precision of 1, such as 11, 12, 13, 14...
    > How can I change the t_clock's precision?


    divide it by 10 ;)
    Mark B, Oct 5, 2005
    #5
  6. Robert

    RamOn Guest

    Hello,

    Under Windows, maybe you can get advantage of using timeGetTime() /
    timeBeginPeriod() / timeEndPeriod() instead of clock()

    Hope this helps

    RamOn


    In article <>,
    says...
    > Hello all,
    >
    > I use clock() to measure the time cost in my program.
    > But the t_clock always be the multiply of 10, for example 10, 20, 30,
    > 40,...
    > And I want to get the precision of 1, such as 11, 12, 13, 14...
    > How can I change the t_clock's precision?
    >
    > Code list below:
    > //-----------------
    > #include <cstdio>
    > #include <fstream.h>
    > #include <ctime>
    >
    > void main() {
    > int x;
    > clock_t t_click,t_start;
    >
    > t_start = clock();
    > for (x=0;x<60;x++) {
    > printf("%d\n",x);
    > }
    >
    > t_click = clock() - t_start;
    > printf("Time Elapsed %d\n",t_click);
    >
    > }
    > //-------------------
    > BTW, I use VC6.0. The environment is P3 600MHz and WinXP.
    >
    > Any suggestions will be appreciated!
    >
    > Best regards,
    > Davy
    >
    >
    RamOn, Oct 5, 2005
    #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. Valentin Tihomirov

    Are clock and divided clock synchronous?

    Valentin Tihomirov, Oct 23, 2003, in forum: VHDL
    Replies:
    11
    Views:
    3,281
    louis lin
    Oct 28, 2003
  2. Szabolcs Nagy
    Replies:
    3
    Views:
    660
  3. Robert

    change clock's precision

    Robert, Oct 5, 2005, in forum: C Programming
    Replies:
    5
    Views:
    359
    RamOn
    Oct 5, 2005
  4. Peger, Daniel H.

    Precision of clock()

    Peger, Daniel H., Aug 30, 2005, in forum: C++
    Replies:
    0
    Views:
    376
    Peger, Daniel H.
    Aug 30, 2005
  5. Peger, Daniel H.

    Precision of clock()

    Peger, Daniel H., Aug 30, 2005, in forum: C++
    Replies:
    1
    Views:
    327
    Marc Mutz
    Aug 30, 2005
Loading...

Share This Page