time() only have a granularity of seconds, so it is only useful for
benchmarking really slow jobs.
On Win32 machines, the simplest method to get subsecond resolution info
is:
DWORD t0 =3D GetTickCount();
// do something
DWORD t1 =3D GetTickCount();
DWORD ms =3D t1 - t0;
GetTickCount() returns wall time, i.e. you must make sure that no other
processes consumes any significant amount of processor time. The
granularity of the returned time is ms - it reports number of milliseconds
since the system booted.
A more advanced timing method is to use GetThreadTimes(). It reports
only time consumed by your thread in kernel and user code. The FILETIME
data type uses a time granularity of 100ns.
Note that other processes/threads will still affect a little bit because
they will fight for room in the instruction and data caches of the
processor, so for best result you should try to keep the processor load
from other programs as low as possible.
FILETIME dummy,krn0,sys0,krn1,sys1;
DWORD64 kernel_us,user_us;
GetThreadTimes(GetCurrentThread(),&dummy,&dummy,&krn0,&sys0);
// Do something
GetThreadTimes(GetCurrentThread(),&dummy,&dummy,&krn1,&sys1);
kernel_us =3D
((DWORD64(krn1.dwHighDateTime)<<32) + DWORD64(krn1.dwLowDateTime))/10 -
((DWORD64(krn0.dwHighDateTime)<<32) + DWORD64(krn0.dwLowDateTime))/10;
system_us =3D
((DWORD64(sys1.dwHighDateTime)<<32) + DWORD64(sys1.dwLowDateTime))/10 -
((DWORD64(sys0.dwHighDateTime)<<32) + DWORD64(sys0.dwLowDateTime))/10;
/Per W
On Mon, 26 Jun 2006, [UTF-8] Aurimas =C4=8Cernius wrote:
> Hi,
>
> > Does anyone have a relatively simple method for timing how long
> > a particular sort takes? I would like to be able to measure
> > in minutes, seconds and hundredths of seconds.
>
> You can simply use time() function from <ctime> to get time before and
> after sort. Then you can simply find a difference between them using
> difftime().
>
> --
> Aurimas
|