clock() problem

Discussion in 'C++' started by Victor Bazarov, Jan 12, 2005.

  1. "Ufit" <> wrote...
    >I must precisely measure the time of calculation.
    > f.ex
    >
    > clock_t begin,finish,time;
    > begin=clock();
    > Calculations();
    > finish=clock();
    > time=finish-begin;
    >
    > but unfortunatelly finish is equal to begin
    > sth like begin=56789 and finish=56789
    > They give me 0. How else can I measure the time very precisely?


    You can do more Calculations:

    begin=clock();
    for (int i=0; i < 1000000; i++)
    Calculations();
    finish=clock();

    or you can use a finer time measurement. Not available in the
    Standard C++, but look in your OS programming manual or ask in
    the newsgroup dedicated to your OS.

    V
    Victor Bazarov, Jan 12, 2005
    #1
    1. Advertising

  2. Victor Bazarov

    Ufit Guest

    I must precisely measure the time of calculation.
    f.ex

    clock_t begin,finish,time;
    begin=clock();
    Calculations();
    finish=clock();
    time=finish-begin;

    but unfortunatelly finish is equal to begin
    sth like begin=56789 and finish=56789
    They give me 0. How else can I measure the time very precisely?
    Please help.
    Thanks

    Ufit
    Ufit, Jan 12, 2005
    #2
    1. Advertising

  3. In Windows you can use GetTickCount() function in Kernel32.dll, just include
    windows.h
    This function retrievs the number of milliseconds since the computer was
    started
    And ofcourse use Victors advice!

    /Rickard
    "Ufit" <> skrev i meddelandet
    news:cs2drs$qr6$...
    > I must precisely measure the time of calculation.
    > f.ex
    >
    > clock_t begin,finish,time;
    > begin=clock();
    > Calculations();
    > finish=clock();
    > time=finish-begin;
    >
    > but unfortunatelly finish is equal to begin
    > sth like begin=56789 and finish=56789
    > They give me 0. How else can I measure the time very precisely?
    > Please help.
    > Thanks
    >
    > Ufit
    Rickard Nisses-Gagnér, Jan 12, 2005
    #3
  4. Victor Bazarov

    Lionel B Guest

    Rickard Nisses-Gagnér wrote:
    >
    > /Rickard
    > "Ufit" <> skrev i meddelandet
    > news:cs2drs$qr6$...
    > > I must precisely measure the time of calculation.
    > > f.ex
    > >
    > > clock_t begin,finish,time;
    > > begin=clock();
    > > Calculations();
    > > finish=clock();
    > > time=finish-begin;
    > >
    > > but unfortunatelly finish is equal to begin
    > > sth like begin=56789 and finish=56789
    > > They give me 0. How else can I measure the time very precisely?
    > > Please help.
    > > Thanks
    > >
    > > Ufit


    <OT (system-dependent)>

    > In Windows you can use GetTickCount() function in Kernel32.dll,
    > just include windows.h
    > This function retrievs the number of milliseconds since the computer
    > was started


    .... but with a resolution of +/- 10ms (depending on your system) I
    believe. On Win NT, QueryPerformanceFrequency() and
    QueryPerformanceCounter() (declared in <winbase.h>) are likely to give
    you much higher resolution.

    </OT>

    > And ofcourse use Victors advice!


    Yes; it will always be unreliable to time just one invocation of a
    (fast) function.

    --
    Lionel B
    Lionel B, Jan 12, 2005
    #4
    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,248
    louis lin
    Oct 28, 2003
  2. Replies:
    4
    Views:
    702
    Peter Alfke
    Apr 27, 2006
  3. Replies:
    5
    Views:
    2,136
    Ricardo
    Jun 23, 2006
  4. himassk
    Replies:
    1
    Views:
    1,217
    Paul Uiterlinden
    May 16, 2007
  5. pankaj.goel
    Replies:
    6
    Views:
    921
    pankaj.goel
    Nov 25, 2008
Loading...

Share This Page