Changeset 3579 in Sophya for trunk/SophyaLib/SysTools/ctimer.cc


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

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.