Changeset 3579 in Sophya for trunk/SophyaLib/SysTools


Ignore:
Timestamp:
Feb 20, 2009, 12:07:02 PM (17 years ago)
Author:
ansari
Message:

Amelioration classe Timer, precision time-elapsed par utilisation gettimeofday(...) au lieu de time(), amelioration calcul temps cpu pour jobs tres long - Reza 20/02/2009

Location:
trunk/SophyaLib/SysTools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/SysTools/ctimer.cc

    r3274 r3579  
    11//
    2 // $Id: ctimer.cc,v 1.7 2007-07-06 12:54:12 ansari Exp $
     2// $Id: ctimer.cc,v 1.8 2009-02-20 11:07:02 ansari Exp $
    33//
    44
     5#include "machdefs.h"
    56#include "sopnamsp.h"
    6 #include "machdefs.h"
    77#include "ctimer.h"
     8
     9#include <iostream>
     10#include <stdio.h>
     11#include <string>
    812
    913//++
     
    5256{
    5357  cpu0 = cpuSplit = clock();
    54   elapse0 = elapseSplit = time(0);
     58  gettimeofday(&elapse0, NULL);
     59  elapseSplit = elapse0;
     60//  elapse0 = elapseSplit = time(0);
    5561  cpuSecT = cpuSecP = 0.;
    5662  elapSecT = elapSecP = 0;
     
    7278void Timer::Split(const char* comm, bool prfg)
    7379{
    74   time_t elapse = time(0);
     80  struct timeval elapse;
     81  gettimeofday(&elapse, NULL);
     82
    7583  clock_t cpu   = clock();
    7684
    77   cpuSecT = ((float)cpu - (float)cpu0) / (float)(CLOCKS_PER_SEC);
    78   cpuSecP = ((float)cpu - (float)cpuSplit) / (float)(CLOCKS_PER_SEC);
     85  uint_8 deltacpu;
     86  if (cpu < cpuSplit) {
     87        clock_t cpumax = 0;   cpumax = ~cpumax;
     88        deltacpu = (uint_8)(cpumax-cpuSplit)+cpu;
     89  }
     90  else deltacpu = cpu - cpuSplit;
    7991 
    80   elapSecP  = elapse - elapseSplit;
    81   elapSecT = elapse - elapse0;
     92  cpuSecT += ((double)deltacpu) / (double)(CLOCKS_PER_SEC);
     93  cpuSecP = ((double)deltacpu) / (double)(CLOCKS_PER_SEC);
     94 
     95  elapSecP  = (elapse.tv_sec - elapseSplit.tv_sec)*1000 
     96              + (elapse.tv_usec - elapseSplit.tv_usec)/1000;
     97  elapSecT = (elapse.tv_sec - elapse0.tv_sec)*1000 
     98              + (elapse.tv_usec - elapse0.tv_usec)/1000;
    8299
    83100
     
    96113// autres C++ que GNU), on fait un cout << chaine.
    97114
    98   int etm  = elapSecP;
    99   int etmt = elapSecT;
    100   char out[200];
    101   sprintf(out,"CPU     Time: Total= %g (Partial= %g) Sec.",
     115  int_4 etm  = elapSecP/1000;
     116  int_4 etmt = elapSecT/1000;
     117
     118  char out[200],outp[64];
     119  sprintf(out,"CPU     Time: Total= %lg (Partial= %lg) s",
    102120          cpuSecT, cpuSecP);
    103121  cout << out << endl;
    104122
    105   sprintf(out,"Elapsed Time: Total= %02d:%02d:%02d (Partial=%02d:%02d:%02d)",
    106           etmt/3600, (etmt%3600)/60, etmt%60,
    107           etm/3600,  (etm%3600)/60,  etm%60);
     123  if (etmt<60) {
     124    int_4 etmtms = elapSecT%1000;
     125    sprintf(out,"%02d.%03d ms", etmt, etmtms);
     126  }
     127  else
     128    sprintf(out,"%02d:%02d:%02d", etmt/3600, (etmt%3600)/60, etmt%60);
    108129
    109   cout << out << endl;
     130  if (etm<60) {
     131    int_4 etmms = elapSecT%1000;
     132    sprintf(outp,"%02d.%03d ms", etm, etmms);
     133  }
     134  else
     135    sprintf(outp,"%02d:%02d:%02d", etm/3600, (etm%3600)/60, etm%60);
     136       
     137  cout << "Elapsed Time: Total=" << out << " (Partial= " << outp << ")" << endl;
    110138}
    111139
  • trunk/SophyaLib/SysTools/ctimer.h

    r3274 r3579  
    11// This may look like C code, but it is really -*- C++ -*-
    22//
    3 // $Id: ctimer.h,v 1.5 2007-07-06 12:54:12 ansari Exp $
     3// $Id: ctimer.h,v 1.6 2009-02-20 11:07:02 ansari Exp $
    44//
    55
     
    1111#include <sys/types.h>
    1212#include <time.h>
    13 #include <iostream>
    14 #include <stdio.h>
     13#include <sys/time.h>
    1514#include <string>
     15
     16using namespace std;
    1617
    1718// <summary> Permet de chronometrer des fonctions. </summary>
     
    4243    between the object creation and the last call to Split().
    4344  */
    44   inline int_8 TotalElapsedTime() { return elapSecT ; }
     45  inline int_8 TotalElapsedTime() { return elapSecT/1000 ; }
    4546  /*! \brief Return the partial elapsed time (number of seconds).
    4647      between the last two calls to Split().
    4748  */
    48   inline int_8 PartialElapsedTime() { return elapSecP ; }
     49  inline int_8 PartialElapsedTime() { return elapSecP/1000 ; }
     50 
     51  /*! \brief Return the total elapsed time (in milli-seconds),
     52    between the object creation and the last call to Split().
     53  */
     54  inline int_8 TotalElapsedTimems() { return elapSecT ; }
     55  /*! \brief Return the partial elapsed time (in milli-seconds).
     56      between the last two calls to Split().
     57  */
     58  inline int_8 PartialElapsedTimems() { return elapSecP ; }
    4959
    5060  /*! \brief Return the total CPU time in seconds,
    5161    between the object creation and the last call to Split().
    5262  */
    53   inline float TotalCPUTime() { return  cpuSecT; }
     63  inline double TotalCPUTime() { return  cpuSecT; }
    5464  /*! \brief Return the partial CPU time in seconds,
    5565      between the last two calls to Split().
    5666  */
    57   inline float PartialCPUTime() { return  cpuSecP; }
     67  inline double PartialCPUTime() { return  cpuSecP; }
    5868
    5969private:
    6070  clock_t cpu0, cpuSplit;
    61   time_t elapse0, elapseSplit;
    62   float cpuSecT, cpuSecP;     // Total and partial CPU time
    63   int_8 elapSecT, elapSecP;    // Total and partial elapsed time
     71  struct timeval elapse0, elapseSplit;
     72  double cpuSecT, cpuSecP;     // Total and partial CPU time
     73  int_8 elapSecT, elapSecP;    // Total and partial elapsed time in ms
    6474  string timerName;
    6575  bool defprtflg;
Note: See TracChangeset for help on using the changeset viewer.