Hi every one,
I have assignment that I am struggling with. Can anybody help me?
The Purpose of the assignment: To evaluate the performance of the vector and list containers of STL for various operations (integer values). In particular, I have to measure the CPU time required for each container to perform the following operations:
(i) Insert all the integers from 0 to 999,999 (one by one) into the back of the container.
(ii) Find the element with value 100,000 and place an iterator to point to that element (you have to manually find the element even for the vector container).
(iii) Erase the element found in part (ii), using the erase function.
For each of the three tasks, output on the screen the CPU time (in seconds) required to complete the task for the two
different containers. In particular, perform each experiment 100 times, and output the average times (don't forget to
clear the containers before each run). Comment on the results and also include a screenshot of the output in your
report.
I have Started with the project and this is what I have so far
Vector Case:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include <iostream>
#include <vector>
using namespace std;
int main()
{
time_t start,stop;
cout << "VECTOR CASE" << endl;
cout << endl;
cout << "Run Times" << "\t" << "Insertion" << endl;
cout << endl;
int counter = 100; //variable counter determines how many tests will be run
for(int x=1; x<=counter; x++)
{
vector<int> my_vector; //create a vector of integers
start=clock();
// Insert element from 0 to 999,999 to the back of the vector
for( int i = 0; i < 1000000; i++ ) {
my_vector.push_back(i);
}
stop=clock();
cout << x << "\t" << "\t" << double(stop-start)/CLOCKS_PER_SEC << endl; // print the time for each run
my_vector.clear(); // clear vector
}
system("pause");
return 0;
}
|
For the List Case:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <iostream>
#include <list>
using namespace std;
int main()
{
time_t start,stop;
cout << "LIST CASE" << endl;
cout << endl;
cout << "Run Times" << "\t" << "Insertion" << endl;
cout << endl;
int count = 100; //variable timesrun determines how many tests will be run
// initialize a list of integers
for(int y=1; y<=count; y++)
{
list<int> list1;
list<int>::iterator position;
// insert the elements of the list from 0 to 999999
start=clock();
for( int i = 0; i < 1000000; i++ ) {
list1.push_back(i);
}
stop=clock();
cout << y << "\t" << "\t" << double(stop-start)/CLOCKS_PER_SEC << endl;
list1.clear();
}
system("pause");
return 0;
}
|
I have been able to insert the values from 0 to 999999, experiment 100 times and print out the time used for each experiment.
Thanks! Your help is greatly appreciated.