lemon/time_measure.h
changeset 2027 119db4e6ab2c
parent 1960 a60b681d0825
child 2028 d0e8a86a1ff2
equal deleted inserted replaced
14:54fb6a9d9559 15:7ff0c57b97f7
    21 
    21 
    22 ///\ingroup timecount
    22 ///\ingroup timecount
    23 ///\file
    23 ///\file
    24 ///\brief Tools for measuring cpu usage
    24 ///\brief Tools for measuring cpu usage
    25 
    25 
       
    26 
       
    27 #ifdef WIN32
       
    28 
       
    29 #include <windows.h>
       
    30 #include <time.h>
       
    31 #include "dos.h"
       
    32 
       
    33 int gettimeofday(struct timeval * tp, struct timezone *) {
       
    34   SYSTEMTIME      systime;
       
    35 
       
    36   if (tp) {
       
    37 
       
    38     struct tm tmrec;
       
    39     time_t theTime = time(NULL);
       
    40 
       
    41     tmrec = *localtime(&theTime);
       
    42     tp->tv_sec = mktime(&tmrec);
       
    43     GetLocalTime(&systime); /* system time */
       
    44 
       
    45     tp->tv_usec = systime.wMilliseconds * 1000;
       
    46 
       
    47   }
       
    48 
       
    49   return 0;
       
    50 
       
    51 }
       
    52 
       
    53 struct tms {
       
    54   long	tms_utime;
       
    55   long	tms_stime;
       
    56   long	tms_cutime;
       
    57   long	tms_cstime;
       
    58 };
       
    59 
       
    60 static long filetime_to_clock(FILETIME *ft)
       
    61 {
       
    62   __int64 qw = ft->dwHighDateTime;
       
    63   qw <<= 32;
       
    64   qw |= ft->dwLowDateTime;
       
    65   qw /= 10000;  /* File time ticks at 0.1uS, clock at 1mS */
       
    66   return (long) qw;
       
    67 
       
    68 }
       
    69 
       
    70 int times(struct tms *tmbuf)
       
    71 {
       
    72   FILETIME create, exit, kernel, user;
       
    73   if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
       
    74     tmbuf->tms_utime = filetime_to_clock(&user);
       
    75     tmbuf->tms_stime = filetime_to_clock(&kernel);
       
    76     tmbuf->tms_cutime = 0;
       
    77     tmbuf->tms_cstime = 0;
       
    78   }
       
    79   else {
       
    80     tmbuf->tms_utime = clock();
       
    81     tmbuf->tms_stime = 0;
       
    82     tmbuf->tms_cutime = 0;
       
    83     tmbuf->tms_cstime = 0;
       
    84   }
       
    85   return 0;
       
    86 }
       
    87 
       
    88 #define _SC_CLK_TCK 1
       
    89 
       
    90 int sysconf(int)
       
    91 {
       
    92   return 1;
       
    93 }
       
    94 
       
    95 #else
       
    96 #include <sys/times.h>
       
    97 #endif
       
    98 
    26 #include <sys/time.h>
    99 #include <sys/time.h>
    27 #include <sys/times.h>
       
    28 #include <fstream>
   100 #include <fstream>
    29 #include <iostream>
   101 #include <iostream>
    30 #include <unistd.h>
   102 #include <unistd.h>
    31 
   103 
    32 namespace lemon {
   104 namespace lemon {
   508   ///        total running time will be written into <tt>*full_time</tt>.
   580   ///        total running time will be written into <tt>*full_time</tt>.
   509   ///\return The average running time of \c f.
   581   ///\return The average running time of \c f.
   510   
   582   
   511   template<class F>
   583   template<class F>
   512   TimeStamp runningTimeTest(F f,double min_time=10,int *num = NULL,
   584   TimeStamp runningTimeTest(F f,double min_time=10,int *num = NULL,
   513 			TimeStamp *full_time=NULL)
   585                             TimeStamp *full_time=NULL)
   514   {
   586   {
   515     Timer t;
   587     Timer t;
   516     TimeStamp full;
   588     TimeStamp full;
   517     int total=0;
   589     int total=0;
   518     for(int tn=1;tn < 1<<30; tn*=2) {
   590     for(int tn=1;tn < 1<<30; tn*=2) {