Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   Related Pages   Examples  

timer.cpp

00001 /* Copyright (c) 2001 C. Grigorescu */
00002 
00003 #include <stdio.h>
00004 #include <sys/time.h>
00005 #include <sys/resource.h>
00006 #include <unistd.h>
00007 #include <stdlib.h>
00008 #include <iostream.h>
00009 #include "timer.h"
00010 #include "misc.h"
00011  
00012 void Timer::Start()
00013 {
00014   if (running) return; 
00015   int e = gettimeofday(&start,NULL);
00016   if (e == 0) 
00017     running = 1;
00018   else 
00019     ERROR("Cannot retrieve system time");
00020 }
00021 
00022 void Timer::Stop()
00023 {
00024   if (!running) return;
00025   int e = gettimeofday(&stop, NULL);
00026   if (e == 0) {
00027     running = 0;
00028 
00029     if (start.tv_usec > stop.tv_usec) {
00030       stop.tv_usec += 1000000;
00031       stop.tv_sec--;
00032     }
00033     seconds      = stop.tv_sec  - start.tv_sec;
00034     microseconds = stop.tv_usec - start.tv_usec;
00035   }
00036   else 
00037     ERROR("Cannot retrieve system time");
00038 }
00039 
00040 void Timer::Tick()
00041 {
00042   if (!running)
00043     return;
00044 
00045   int e = gettimeofday(&stop, NULL);
00046   if (e == 0) {
00047     if (start.tv_usec > stop.tv_usec) {
00048       stop.tv_usec += 1000000;
00049       stop.tv_sec--;
00050     }
00051     seconds      = stop.tv_sec  - start.tv_sec;
00052     microseconds = stop.tv_usec - start.tv_usec;
00053     
00054     cout.fill('0');
00055     cout << "Elapsed time : \t" << seconds << ".";
00056     cout.width(3);
00057     cout << microseconds/1000 << " sec. " << endl;
00058   }
00059   else 
00060     ERROR("Cannot retrieve system time");
00061 }
00062 
00063 void Timer::Print()
00064 {  
00065   cout.fill('0');
00066   cout << "Elapsed time : \t" << seconds << ".";
00067   cout.width(3);
00068   cout << microseconds/1000 << " sec. " << endl;
00069 }
00070 
00071 float Timer::getTime()
00072 {
00073   if (!running)
00074     return 0;
00075 
00076   int e = gettimeofday(&stop, NULL);
00077 
00078   if (e == 0) {
00079     if (start.tv_usec > stop.tv_usec) {
00080       stop.tv_usec += 1000000;
00081       stop.tv_sec--;
00082     }
00083     seconds      = stop.tv_sec  - start.tv_sec;
00084     microseconds = stop.tv_usec - start.tv_usec;
00085   
00086     return (seconds + (float)(microseconds)/1000000.0);
00087   }
00088   else
00089     return 0;
00090 }
00091 
00092 void MemoryStatistics()
00093 {
00094   rusage res_usage;
00095   int e = getrusage(RUSAGE_SELF, &res_usage);
00096   if (e == 0) {
00097     long total = res_usage.ru_ixrss +  res_usage.ru_idrss +  res_usage.ru_isrss ;
00098     cout << "Memory used: " << total << "\t from which: " << endl;
00099     cout << "\t " << res_usage.ru_ixrss << " - shared " << endl;
00100     cout << "\t " << res_usage.ru_idrss << " - unshared " << endl;
00101     cout << "\t " << res_usage.ru_isrss << " - stack " << endl;
00102     cout << "\t " << res_usage.ru_maxrss << " -  " << endl;
00103   }
00104   else
00105     ERROR("Cannot retrieve memory statistics");
00106 }