COIN-OR::LEMON - Graph Library

Ticket #74: mingw_compatible_time_measure.patch

File mingw_compatible_time_measure.patch, 6.6 KB (added by Balazs Dezso, 17 years ago)
  • lemon/time_measure.h

    # HG changeset patch
    # User Balazs Dezso <deba@inf.elte.hu>
    # Date 1208162801 -7200
    # Node ID 0da8b7227c4b07351040b305265da9e117ddcbf3
    # Parent  ae7785fe84315de6be35a182420b6369e851f00d
    MinGW compatible time measure
    
    diff -r ae7785fe8431 -r 0da8b7227c4b lemon/time_measure.h
    a b  
    2323///\file
    2424///\brief Tools for measuring cpu usage
    2525
     26#ifdef WIN32
     27#include <windows.h>
     28#include <cmath>
     29#else
    2630#include <sys/times.h>
     31#include <sys/time.h>
     32#endif
    2733
    28 #include <sys/time.h>
    2934#include <fstream>
    3035#include <iostream>
    31 #include <unistd.h>
    3236
    3337namespace lemon {
    3438
    namespace lemon { 
    5458
    5559  class TimeStamp
    5660  {
    57     struct rtms
    58     {
    59       double tms_utime;
    60       double tms_stime;
    61       double tms_cutime;
    62       double tms_cstime;
    63       rtms() {}
    64       rtms(tms ts) : tms_utime(ts.tms_utime), tms_stime(ts.tms_stime),
    65                      tms_cutime(ts.tms_cutime), tms_cstime(ts.tms_cstime) {}
    66     };
    67     rtms ts;
    68     double real_time;
     61    double utime;
     62    double stime;
     63    double cutime;
     64    double cstime;
     65    double rtime;
    6966 
    70     rtms &getTms() {return ts;}
    71     const rtms &getTms() const {return ts;}
    72 
    7367    void _reset() {
    74       ts.tms_utime = ts.tms_stime = ts.tms_cutime = ts.tms_cstime = 0;
    75       real_time = 0;
     68      utime = stime = cutime = cstime = rtime = 0;
    7669    }
    7770
    7871  public:
    7972
     73#ifndef WIN32
    8074    ///Read the current time values of the process
    8175    void stamp()
    8276    {
    8377      timeval tv;
    84       tms _ts;
    85       times(&_ts);
    86       gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
    87       ts=_ts;
     78      gettimeofday(&tv, 0);
     79      rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
     80
     81      tms ts;
     82      double tck=sysconf(_SC_CLK_TCK);
     83      times(&ts);
     84      utime=ts.tms_utime/tck;
     85      stime=ts.tms_stime/tck;
     86      cutime=ts.tms_cutime/tck;
     87      cstime=ts.tms_cstime/tck;
    8888    }
     89#else
     90    void stamp() {
     91      static const double ch = 4294967296.0e-7;
     92      static const double cl = 1.0e-7;
     93
     94      FILETIME system;
     95      GetSystemTimeAsFileTime(&system);
     96      rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
     97
     98      FILETIME create, exit, kernel, user;
     99      if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
     100        utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
     101        stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
     102        cutime = 0;
     103        cstime = 0;
     104      } else {
     105        rtime = 0;
     106        utime = 0;
     107        stime = 0;
     108        cutime = 0;
     109        cstime = 0;
     110      }
     111    }
     112#endif     
    89113 
    90114    /// Constructor initializing with zero
    91115    TimeStamp()
    namespace lemon { 
    99123    ///\e
    100124    TimeStamp &operator+=(const TimeStamp &b)
    101125    {
    102       ts.tms_utime+=b.ts.tms_utime;
    103       ts.tms_stime+=b.ts.tms_stime;
    104       ts.tms_cutime+=b.ts.tms_cutime;
    105       ts.tms_cstime+=b.ts.tms_cstime;
    106       real_time+=b.real_time;
     126      utime+=b.utime;
     127      stime+=b.stime;
     128      cutime+=b.cutime;
     129      cstime+=b.cstime;
     130      rtime+=b.rtime;
    107131      return *this;
    108132    }
    109133    ///\e
    namespace lemon { 
    115139    ///\e
    116140    TimeStamp &operator-=(const TimeStamp &b)
    117141    {
    118       ts.tms_utime-=b.ts.tms_utime;
    119       ts.tms_stime-=b.ts.tms_stime;
    120       ts.tms_cutime-=b.ts.tms_cutime;
    121       ts.tms_cstime-=b.ts.tms_cstime;
    122       real_time-=b.real_time;
     142      utime-=b.utime;
     143      stime-=b.stime;
     144      cutime-=b.cutime;
     145      cstime-=b.cstime;
     146      rtime-=b.rtime;
    123147      return *this;
    124148    }
    125149    ///\e
    namespace lemon { 
    131155    ///\e
    132156    TimeStamp &operator*=(double b)
    133157    {
    134       ts.tms_utime*=b;
    135       ts.tms_stime*=b;
    136       ts.tms_cutime*=b;
    137       ts.tms_cstime*=b;
    138       real_time*=b;
     158      utime*=b;
     159      stime*=b;
     160      cutime*=b;
     161      cstime*=b;
     162      rtime*=b;
    139163      return *this;
    140164    }
    141165    ///\e
    namespace lemon { 
    148172    ///\e
    149173    TimeStamp &operator/=(double b)
    150174    {
    151       ts.tms_utime/=b;
    152       ts.tms_stime/=b;
    153       ts.tms_cutime/=b;
    154       ts.tms_cstime/=b;
    155       real_time/=b;
     175      utime/=b;
     176      stime/=b;
     177      cutime/=b;
     178      cstime/=b;
     179      rtime/=b;
    156180      return *this;
    157181    }
    158182    ///\e
    namespace lemon { 
    173197    ///Gives back the user time of the process
    174198    double userTime() const
    175199    {
    176       return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
     200      return utime;
    177201    }
    178202    ///Gives back the system time of the process
    179203    double systemTime() const
    180204    {
    181       return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
     205      return stime;
    182206    }
    183207    ///Gives back the user time of the process' children
     208
     209    ///\note On <tt>WIN32</tt> platform this value is not calculated.
     210    ///
    184211    double cUserTime() const
    185212    {
    186       return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
     213      return cutime;
    187214    }
    188215    ///Gives back the user time of the process' children
     216
     217    ///\note On <tt>WIN32</tt> platform this value is not calculated.
     218    ///
    189219    double cSystemTime() const
    190220    {
    191       return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
     221      return cstime;
    192222    }
    193223    ///Gives back the real time
    194     double realTime() const {return real_time;}
     224    double realTime() const {return rtime;}
    195225  };
    196226
    197227  TimeStamp operator*(double b,const TimeStamp &t)
    namespace lemon { 
    212242  /// \li \c cs: system cpu time of children,
    213243  /// \li \c real: real time.
    214244  /// \relates TimeStamp
     245  /// \note On <tt>WIN32</tt> platform the cummulative values are not
     246  /// calculated.
    215247  inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
    216248  {
    217     long cls = sysconf(_SC_CLK_TCK);
    218     os << "u: " << double(t.getTms().tms_utime)/cls <<
    219       "s, s: " << double(t.getTms().tms_stime)/cls <<
    220       "s, cu: " << double(t.getTms().tms_cutime)/cls <<
    221       "s, cs: " << double(t.getTms().tms_cstime)/cls <<
     249    os << "u: " << t.userTime() <<
     250      "s, s: " << t.systemTime() <<
     251      "s, cu: " << t.cUserTime() <<
     252      "s, cs: " << t.cSystemTime() <<
    222253      "s, real: " << t.realTime() << "s";
    223254    return os;
    224255  }
    namespace lemon { 
    404435      return operator TimeStamp().systemTime();
    405436    }
    406437    ///Gives back the ellapsed user time of the process' children
     438
     439    ///\note On <tt>WIN32</tt> platform this value is not calculated.
     440    ///
    407441    double cUserTime() const
    408442    {
    409443      return operator TimeStamp().cUserTime();
    410444    }
    411445    ///Gives back the ellapsed user time of the process' children
     446
     447    ///\note On <tt>WIN32</tt> platform this value is not calculated.
     448    ///
    412449    double cSystemTime() const
    413450    {
    414451      return operator TimeStamp().cSystemTime();