Question

In: Computer Science

Conduct an evaluation of the performance of STL containers (vector, list, set, unordered_set) in inserting and...

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)

  1. create a vector of 100,000 elements of integers, with the values initially set as 1~100,000, in this order.
  2. randomize the ordering of these values so they are shuffled
  3. measure the time of inserting all elements of this vector into another empty vector, list, set, and unordered_set, at the end of the container
    • that is, the time of populating 100K numbers in the 4 containers, respectively (4 resultant time durations)
    • you can write a universal function that does inserting at the iterator position returned by c.end(), where c is the container in question
  4. do the same for the 4 containers (also from the empty state), but inserting elements at the beginning of the container (inserting at .begin())
  5. with all containers properly populated, measure the time to find each value of (1~10,000, that's 10K) in vector, list, set, and unordered_set
    • that is, the time of finding all 10K numbers in the 4 containers, respectively (4 resultant time durations)
    • for vector/list, use the std::find() algorithm; for set/unordered_set, use the member function .find()
  6. when reporting your results, please also output the percentage relative to data for the vector

Solutions

Expert Solution

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.


Related Solutions

Language: C++ Conduct an evaluation of the performance of STL containers (vector, list, set, unordered_set) in...
Language: C++ 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) create a vector of 100,000 elements of integers, with the values initially set as 1~100,000, in this order. randomize the ordering...
Discuss tradeoffs between using the C++ STL list and vector.
Discuss tradeoffs between using the C++ STL list and vector.
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is selected from the online contest problem archive (Links to an external site.), which is used mostly by college students world wide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest (Links to an external site.). For your convenience, I copied the description of the problem below with my note on the...
List the performance evaluation measures. When is it appropriate to use them?
List the performance evaluation measures. When is it appropriate to use them?
write a one-page paper on how you would conduct a performance evaluation about an investment into...
write a one-page paper on how you would conduct a performance evaluation about an investment into a new market.
STL vector C++ /* ---- OUTPUT ---- Enter test scores (-1 to quit) Enter a score:  77...
STL vector C++ /* ---- OUTPUT ---- Enter test scores (-1 to quit) Enter a score:  77 Enter a score:  83 Enter a score:  99 Enter a score:  67 Enter a score:  88 Enter a score:  -1 The average score is 82.8. Write a program in C++ that prompts the user to   Enter test scores (-1 to quit)    (see output) The program calculates and displays   the average of the test scores. Use a vector to hold the values double data type Pass the vector to a...
Write the following algorithms for a Doubly Linked List Inserting an item                              
Write the following algorithms for a Doubly Linked List Inserting an item                                                                                                                              [7] Deleting an item                                                                                                                               [7] Question two Take a queue containing numbers 10, 15, 5, 25, 30 in which 30 has been inserted first. After performing the following operations, what would be the contents of the queue? Delete two elements                                                                                                                      [2] Insert 7 and then 20                                                                                                                        [2] Delete an element                                                                                                                          [2]
Write a short evaluation of performance appraisal
Write a short evaluation of performance appraisal
Using only core C++ (no special libraries, except STL vector or string if you want), write...
Using only core C++ (no special libraries, except STL vector or string if you want), write a C++ program that allows a user to input a string and (a) Checks if the expression is a valid polynomial. Parentheses or negation are not allowed. Spaces should be ignored. E.g., the following are valid i. n^2+2*n+5 ii. 2*n + 4.54* n^5 +4 +5*n and the following are invalid iii. n^3n iv. n^4.2 v. 5n vi. n^3 -3*n if an input is given...
How to write a max-heap implementation of a templated priority queue class using the STL std::vector...
How to write a max-heap implementation of a templated priority queue class using the STL std::vector class to hold the data array
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT