Velocity Reviews > C++ > clock() problem

clock() problem

Victor Bazarov
Guest
Posts: n/a

 01-12-2005
"Ufit" <(E-Mail Removed)> 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

Ufit
Guest
Posts: n/a

 01-12-2005
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?
Thanks

Ufit

Rickard Nisses-Gagnér
Guest
Posts: n/a

 01-12-2005
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

/Rickard
"Ufit" <(E-Mail Removed)> skrev i meddelandet
news:cs2drs\$qr6\$(E-Mail Removed)...
> 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?
> Thanks
>
> Ufit

Lionel B
Guest
Posts: n/a

 01-12-2005
Rickard Nisses-Gagnér wrote:
>
> /Rickard
> "Ufit" <(E-Mail Removed)> skrev i meddelandet
> news:cs2drs\$qr6\$(E-Mail Removed)...
> > 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?
> > 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