CPU time in Windows

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
Jim Ablett
Posts: 1383
Joined: Fri Jul 14, 2006 7:56 am
Location: London, England
Full name: Jim Ablett

Re: CPU time in Windows

Post by Jim Ablett »

This is a better solution to Mingw32 cpu time bug >

Code: Select all

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

static void
__winnt_cpu_time &#40;long *sec, long *usec&#41;
&#123;
  union &#123;
    FILETIME ft;
    unsigned long long ulltime;
  &#125; kernel_time,  user_time;

  FILETIME unused1, unused2;
  unsigned long long total_time;

  /* No support for Win9x.  The high order bit of the DWORD
     returned by GetVersion is 0 for NT and higher. */
  if &#40;GetVersion () >= 0x80000000&#41;
    &#123;
      *sec = -1;
      *usec = 0;
      return;
    &#125;

  /* The FILETIME structs filled in by GetProcessTimes represent
     time in 100 nanosecond units. */
  GetProcessTimes &#40;GetCurrentProcess (), &unused1, &unused2,
              	   &kernel_time.ft, &user_time.ft&#41;;
      
  total_time = &#40;kernel_time.ulltime + user_time.ulltime&#41;/10; 
  *sec = total_time / 1000000;
  *usec = total_time % 1000000;
&#125;

Jim.
Daniel Shawul
Posts: 4185
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: CPU time in Windows

Post by Daniel Shawul »

I am surprized though why unix clock() measures the cpu time. C++ seems to define clock() as a measure of wall clock time http://www.cplusplus.com/reference/clib ... ime/clock/. It is mentioned that the reference could be different on different systems but nothing about some systems excluding I/O time.
User avatar
hgm
Posts: 27790
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: CPU time in Windows

Post by hgm »

Jim, could you make another Win-32 compile of Fruit for me? I have now increased the precision of the timers to 0.1 msec, for both wall-clock and CPU time.

Just use the file at http://hgm.nubati.net/protocol.cpp in stead of the normal one, that should do it.

I plan to distribute this version of Fruit 2.1 with the WinBoard binary package, and perhaps also make a separate 'timing kit' for people wanting to evaluate GUI performance, with this Fruit, a similarly adapted Fairy-Max and a tool to extract the timing lines from a debug file, and print the timing stats.
User avatar
Jim Ablett
Posts: 1383
Joined: Fri Jul 14, 2006 7:56 am
Location: London, England
Full name: Jim Ablett

Re: CPU time in Windows

Post by Jim Ablett »

User avatar
hgm
Posts: 27790
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: CPU time in Windows

Post by hgm »

Wow, you are fast! :D Thanks!
rbarreira
Posts: 900
Joined: Tue Apr 27, 2010 3:48 pm

Re: CPU time in Windows

Post by rbarreira »

Daniel Shawul wrote:I am surprized though why unix clock() measures the cpu time. C++ seems to define clock() as a measure of wall clock time http://www.cplusplus.com/reference/clib ... ime/clock/. It is mentioned that the reference could be different on different systems but nothing about some systems excluding I/O time.
Actually that page is even more confusing, as it talks about "processing times" as well.
Daniel Shawul
Posts: 4185
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: CPU time in Windows

Post by Daniel Shawul »

Actually that page is even more confusing, as it talks about "processing times" as well.
That page is consistent with msvc implementation. OTOH glibc's implementation is weird for me.

Code: Select all

/***
*clock_t clock&#40;) - Return the processor time used by this process.
*
*Purpose&#58;
*       This routine calculates how much time the calling process
*       has used.  At startup time, startup calls __inittime which stores
*       the initial time.  The clock routine calculates the difference
*       between the current time and the initial time.
*
*       Clock must reference _cinitime so that _cinitim.asm gets linked in.
*       That routine, in turn, puts __inittime in the startup initialization
*       routine table.
*
*Entry&#58;
*       No parameters.
*       itime is a static structure of type timeb.
*
*Exit&#58;
*       If successful, clock returns the number of CLK_TCKs &#40;milliseconds&#41;
*       that have elapsed.  If unsuccessful, clock returns -1.
*
*Exceptions&#58;
*       None.
*
*******************************************************************************/

clock_t __cdecl clock (
        void
        )
&#123;
        unsigned __int64 current_tics;
        FILETIME ct;

        GetSystemTimeAsFileTime&#40; &ct );

        current_tics = &#40;unsigned __int64&#41;ct.dwLowDateTime +
                       ((&#40;unsigned __int64&#41;ct.dwHighDateTime&#41; << 32&#41;;

        /* calculate the elapsed number of 100 nanosecond units */
        current_tics -= start_tics;

        /* return number of elapsed milliseconds */
        return &#40;clock_t&#41;&#40;current_tics / 10000&#41;;
&#125;