Skip to main content

insertlist.cpp


/*
   Program to benchmark insertion at the beginning of a list.
   10000 insertions are done by default; a different number can be
   specified as a command line argument.
   NDE, 2013-02-02
*/
#include <list>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <ctime>
using namespace std;
int main(int argc, char* argv[])
{
  // Parse command line
  int count = 10000;
  if (argc > 1) {
    istringstream arg(argv[1]);
    arg >> count;
  }
  // Perform insertions at front of container
  cout << "Performing " << count << " insertions..." << endl;
  list<int> container;
  clock_t start = clock();
  for (int i = 0; i < count; ++i) {
    container.insert(container.begin(), i);
  }
  // Compute and display approximate elapsed CPU time
  clock_t end = clock();
  double elapsed = static_cast<double>(end - start) / CLOCKS_PER_SEC;
  cout << "Time: "
       << fixed << setprecision(2)
       << elapsed << " seconds" << endl;
  return 0;
}