// Department of Computing, Imperial College London
// Operating Systems Concepts 
// Olav Beckmann, with acknowledgements to Paul Kelly
// Tutorial 1 - heart.cc
// 
// Compile this program with g++ -o heart heart.cc . 
// Do *not* use optimisation when compiling.

#include <sys/time.h>
#include <iostream>

using namespace std;

double time_in_seconds( ) { 
  double result;
  struct timeval tv; 
  struct timezone tz;
  gettimeofday( &tv, &tz );
  result =  (double) tv.tv_sec;
  result += ((double) tv.tv_usec / 1000000.0);
  return result;
}


int main( int argc, char *argv[] ) {
  int r = 0;
  double current_time = time_in_seconds();
  double time_at_start = current_time;
  for( int i = 0; i < 100; i++ ) {
    // Waste some time!
    for( int j = 0; j < 100000000; j++ ) {
      r += j;
    }
    cerr << "Iteration " << i << " took " 
	 << time_in_seconds() - current_time << " seconds.\n";
    current_time = time_in_seconds();
  }
  double time_at_finish = time_in_seconds();
  cout << "Overall time taken was " 
       << (time_at_finish - time_at_start) << "." << endl;
  return 0;
}

