# clock() problem

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

1. ### Victor BazarovGuest

"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

2. ### UfitGuest

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

3. ### Rickard Nisses-GagnérGuest

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
4. ### Lionel BGuest

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

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