Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 30th, 2012  |  syntax: None  |  size: 0.92 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Adding a timer in a C   loop
  2. #include <cstdio>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.    clock_t start = clock();
  9.    /* Code you want timed here */
  10.    printf("Time elapsed: %fn", ((double)clock() - start) / CLOCKS_PER_SEC);
  11. }
  12.        
  13. #include <sys/time.h>
  14. class CBenchmark
  15. {
  16.     public:
  17.         CBenchmark(void) throw() :
  18.             m_startTime(0),m_finalTime(0)
  19.     {}
  20.  
  21.         void start(void) throw()
  22.         {
  23.             m_startTime=getTime();
  24.             return;
  25.         }
  26.  
  27.         void stop(void) throw()
  28.         {
  29.             m_finalTime=getTime();
  30.             return;
  31.         }
  32.  
  33.         size_t elapsedTime(void) throw()
  34.         {
  35.             return m_finalTime-m_startTime;
  36.         }
  37.     protected:
  38.         size_t getTime(void) throw()
  39.         {
  40.             timeval tp;
  41.             gettimeofday(&tp,NULL);
  42.             return tp.tv_sec*1e6+tp.tv_usec;
  43.         }
  44.  
  45.         size_t m_startTime;
  46.         size_t m_finalTime;
  47. };