- Main Page
- Classes
- Files

- File List

# OptFrame/Parallel/MapReduce++/util/Timer.hpp

    00001 #ifndef TIMER_HPP
    00002 #define TIMER_HPP
    00003
    00004 #include <cstdio>
    00005 #include <cstdlib>
    00006 #include <sys/time.h>
    00007
    00008 class Timer
    00009 {
    00010 protected:
    00011         struct timeval time1, time2;
    00012     long elapsed_utime;    /* elapsed time in microseconds */
    00013     long elapsed_mtime;    /* elapsed time in milliseconds */
    00014     long elapsed_seconds;  /* diff between seconds counter */
    00015     long elapsed_useconds; /* diff between microseconds counter */
    00016
    00017         bool showMessageOnDestroy;
    00018 public:
    00019         Timer()
    00020         {
    00021                 gettimeofday(&time1, NULL);
    00022                 showMessageOnDestroy=true;
    00023         };
    00024         Timer(bool m)
    00025         {
    00026                 gettimeofday(&time1, NULL);
    00027                 showMessageOnDestroy=m;
    00028         };
    00029         virtual ~Timer(){ if(showMessageOnDestroy) printf("Elapsed time: %.3f secs\n", now()); };
    00030         virtual double now()
    00031         {
    00032                 gettimeofday(&time2, NULL);
    00033             elapsed_seconds  = time2.tv_sec  - time1.tv_sec;
    00034             elapsed_useconds = time2.tv_usec - time1.tv_usec;
    00035             elapsed_utime = (elapsed_seconds) * 1000000 + elapsed_useconds;
    00036             elapsed_mtime = ((elapsed_seconds) * 1000 + elapsed_useconds/1000.0) + 0.5;
    00037                 return ( elapsed_mtime / 1000 + ( elapsed_mtime % 1000 ) * 0.001 ) ;
    00038         };
    00039 };
    00040
    00041 /* Old version
    00042 #include <ctime>
    00043
    00044 class Timer
    00045 {
    00046 protected:
    00047         clock_t initialTime;
    00048         bool showMessageOnDestroy;
    00049 public:
    00050         Timer(){ initialTime=clock(); showMessageOnDestroy=true; };
    00051         Timer(bool m){ initialTime=clock(); showMessageOnDestroy=m; };
    00052
    00053         virtual ~Timer(){ if(showMessageOnDestroy) printf("Spent time: %.3f secs\n", (double) (clock() - initialTime) / CLOCKS_PER_SEC ); };
    00054         virtual double now() { return ((double)(clock() - initialTime) / CLOCKS_PER_SEC) ; };
    00055 };*/
    00056
    00057 #endif /* TIMER_HPP */

All Classes Functions