Miscellaneous

#include <time.h>
#include <stdio.h>

/*______________________________________________________________________________
| This timer class acts like a "stopwatch", returning elapsed seconds.         |
| Our current resolution on Linux is about 0.01 seconds.                       |
|                                                                              |
| Required libraries: <time.h>                                                 |
|_____________________________________________________________________________*/
class timer
{
 public:
  timer() : _elapsed_time(0)
   {};

  void reset()
   { _elapsed_time = 0; }

  // Start the clock
  void start()
   { _start_time = clock(); }

  // Stop the clock and return cumulative elapsed time (in seconds).
  double stop()
    {
   _elapsed_time += clock() - _start_time;
   return (double)_elapsed_time / CLOCKS_PER_SEC;
    }

  // Return time accumulated so far.
  double elapsed() const
    { return (double)_elapsed_time / CLOCKS_PER_SEC; }

 private:
  clock_t _start_time;
  clock_t _elapsed_time;

}; // timer

// Test the timer
main()
{
 timer timer1;

 timer1.start();

 for (long i = 0; i < 10000000; i++);

 timer1.stop();

 printf("Total seconds <%f>\n", timer1.elapsed());
}