In: Computer Science
Conduct an evaluation of the performance of STL containers (vector, list, set, unordered_set) in inserting and finding elements, with a relatively large data set. What you'll be measuring is the execution time. Obviously it's dependent on the speed of the computer one uses, so what matters is the relative speed (relative to the vector's speed in percentage)
Explanation:-
Summary :
As part of the solution provided following (i) Notes , (ii) Code & (iii) Output
Notes :
Implemented function InsertAt() which accepts the initial vector and the stl object (set, vector, list or uset ) & insertAtBegin to inserts the 100k elements and prints the time taken .
Initially vector is inserted with 1-100k and later shuffled using 2 random numbers generated for 50k times and swapped the elements at those random indices. Next utilizing the function insertAt() all these shuffled elements are inserted into the respective stl objects. finally using the find() functions , search and print the time elapsed for each of the operations.
#################### Code ######################
#include <iostream>
#include <vector>
#include <iomanip>
#include <set>
#include <stdlib.h>
#include <time.h>
#include <chrono>
#include <list>
#include<unordered_set>
#include <typeinfo>
#include <algorithm>
#define TOTAL_ELEMENTS 100000
#define SEARCH_NUM 10000
using namespace std;
// Function to insert given elements using flag
insertAtBegin
template <class T>
void insertAt( vector<int> input_vect , T &stlObj , bool
insertAtBegin )
{
string str ;
auto stime = chrono::steady_clock::now();
if ( insertAtBegin )
{
str = "begin";
for (auto it = input_vect.begin(); it != input_vect.end();
++it)
auto itr = stlObj.insert( stlObj.begin(), *it );
}else
{
str = "end";
for (auto it = input_vect.begin(); it != input_vect.end();
++it)
auto itr = stlObj.insert( stlObj.end(), *it );
}
auto etime = chrono::steady_clock::now();
//cout.precision(dbl::max_digits10);
cout << " Insert at " << str << " for type "
<< typeid(stlObj).name() << " time - " <<
fixed
<< std::setprecision (15) <<
chrono::duration_cast<chrono::nanoseconds>(etime - stime
).count() << " nano sec \n";
}
int main()
{
srand (time(NULL));
vector<int> init_vect ;
auto stime = chrono::steady_clock::now();
for( int i=1; i <= TOTAL_ELEMENTS ; i++ )
init_vect.insert(init_vect.end(),i);
auto etime = chrono::steady_clock::now();
cout << " Initial Vector creation time - " << fixed
<< std::setprecision (15) <<
chrono::duration_cast<chrono::nanoseconds>(etime - stime
).count() << " nano sec \n";
// Shuffle the Vector elements
stime = chrono::steady_clock::now();
int idx1 , idx2 , temp;
for( int j = 0 ; j < TOTAL_ELEMENTS/2 ; j++ )
{
idx1 = rand() % TOTAL_ELEMENTS ;
idx2 = rand() % TOTAL_ELEMENTS ;
if ( idx1 != idx2 )
{
temp = init_vect[idx1] ;
init_vect[idx1] = init_vect[idx2];
init_vect[idx2] = temp;
}
}
etime = chrono::steady_clock::now();
// Calculate the delta between end and start time and print with
precision 15
cout << " Shuffling Vector time - " << fixed <<
std::setprecision (15) <<
chrono::duration_cast<chrono::nanoseconds>(etime - stime
).count() << " nano sec \n";
// Insert at the begin for all stl objects
vector<int> vect1 ;
insertAt(init_vect, vect1 ,true);
set<int> set1 ;
insertAt(init_vect, set1 , true );
list<int> list1 ;
insertAt(init_vect, list1 , true );
unordered_set<int> uset1 ;
insertAt(init_vect, uset1 , true );
cout << "\n\n";
// Insert at the end for all …
OUTPUT:
If you have any dought about this answer dont give dislike
,tell us your dought in the comment then i can explain, Please rate
me by giving me a like or thumb because it motivates me to do more
work,Thank you.