synaps/base/chrono.h

00001 /********************************************************************
00002  *   This file is part of the source code of the SYNAPS library.
00003  *   Author(s): J.P. Pavone, GALAAD, INRIA
00004  *   $Id: chrono.hpp,v 1.1 2005/07/11 10:03:57 jppavone Exp $
00005  ********************************************************************/
00006 #ifndef SYNAPS_UTIL_CHRONO_H
00007 #define SYNAPS_UTIL_CHRONO_H
00008 //--------------------------------------------------------------------
00009 #include <sys/time.h>
00010 #include <iostream>
00011 #include <synaps/init.h>
00012 //--------------------------------------------------------------------
00013 __BEGIN_NAMESPACE_SYNAPS
00014 //--------------------------------------------------------------------
00015 
00016 #include <stdlib.h>
00017 #include <sys/time.h>
00018 #include <unistd.h>
00019 #include <synaps/init.h>
00020 
00021 #define time()\
00022 ({\
00023   struct timeval t;\
00024   gettimeofday(&t,NULL);\
00025   ((double)(t.tv_sec*1000000+t.tv_usec))/((double)1000000);\
00026 })
00027 
00028 
00029 struct chrono 
00030 {
00031   bool stopped;
00032   struct timeval data0;
00033   struct timeval data1;
00034   chrono() { stopped = true; };
00035   inline void start()
00036   { 
00037     stopped = false;
00038     gettimeofday(&data0,0);
00039   };
00040   
00041   inline void stop()
00042   { 
00043     gettimeofday(&data1,0);
00044     stopped = true; 
00045   };
00046   unsigned val() {
00047     //int secs = data1.tv_sec-data0.tv_sec;
00048     //      int micros = data1.tv_usec - data0.tv_usec;
00049     return std::abs(data1.tv_sec-data0.tv_sec)*1000000+data1.tv_usec - data0.tv_usec; 
00050   };
00051   
00052     bool ok() {
00053       int secs = data1.tv_sec-data0.tv_sec;
00054       return secs> 0;
00055     };
00056   
00057   inline void write( std::ostream& out )
00058   {
00059     int micros,secnds,millis,minutes;
00060     micros  = (data1.tv_sec-data0.tv_sec)*1000000+
00061         data1.tv_usec - data0.tv_usec;
00062     secnds  = micros / 1000000;
00063     micros  = micros % 1000000;
00064     millis  = micros / 1000;
00065     micros  = micros % 1000;
00066     minutes = secnds / 60;
00067     secnds  = secnds % 60;
00068       if ( minutes )
00069         out << 
00070           minutes << " mn "; 
00071       if ( secnds )
00072         out <<  
00073           secnds << " seconds ";
00074       if ( millis )
00075         out << 
00076           millis << " ms ";
00077       out << micros << " us \n";
00078     };
00079 };
00080 
00081 //--------------------------------------------------------------------
00082 __END_NAMESPACE_SYNAPS
00083 /********************************************************************/
00084 #endif //

SYNAPS DOCUMENTATION
logo