Question

In: Computer Science

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)

  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

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 stl objects 
        vector<int> vect2 ;
        insertAt(init_vect, vect2 ,false);
        
        set<int> set2 ;
        insertAt(init_vect, set2 , false );
        
        list<int> list2 ;
        insertAt(init_vect, list2 , false );
        
        unordered_set<int> uset2 ;
        insertAt(init_vect, uset2 , false );
        
        cout << "\n\n";
        
        // find 1-10k elements in each of the stl 

        stime = chrono::steady_clock::now();
        for( int i = 1 ; i <= SEARCH_NUM ; i++ )
                auto it = find(vect1.begin(), vect1.end(), i); 
        etime = chrono::steady_clock::now();
        
        
        cout << " Searching elements in Vector1 time - " << chrono::duration_cast<chrono::nanoseconds>(etime - stime ).count() << " nano sec \n";
        
        stime = chrono::steady_clock::now();
        for( int i = 1 ; i <= SEARCH_NUM ; i++ )
                auto it = find(vect2.begin(), vect2.end(), i); 
        etime = chrono::steady_clock::now();
        
        
        cout << " Searching elements in Vector2 time - " << chrono::duration_cast<chrono::nanoseconds>(etime - stime ).count() << " nano sec \n";

        stime = chrono::steady_clock::now();
        for( int i = 1 ; i <= SEARCH_NUM ; i++ )
                auto it = find(list1.begin(), list1.end(), i); 
        etime = chrono::steady_clock::now();
        
        
        cout << " Searching elements in List1 time - " << chrono::duration_cast<chrono::nanoseconds>(etime - stime ).count() << " nano sec \n";
        
        stime = chrono::steady_clock::now();
        for( int i = 1 ; i <= SEARCH_NUM ; i++ )
                auto it = find(list2.begin(), list2.end(), i); 
        etime = chrono::steady_clock::now();
        
        
        cout << " Searching elements in List2 time - " << chrono::duration_cast<chrono::nanoseconds>(etime - stime ).count() << " nano sec \n";

        stime = chrono::steady_clock::now();
        for( int i = 1 ; i <= SEARCH_NUM ; i++ )
                auto it = set1.find(i); 
        etime = chrono::steady_clock::now();
        
        
        cout << " Searching elements in Set1 time - " << chrono::duration_cast<chrono::nanoseconds>(etime - stime ).count() << " nano sec \n";


        stime = chrono::steady_clock::now();
        for( int i = 1 ; i <= SEARCH_NUM ; i++ )
                auto it = set2.find(i); 
        etime = chrono::steady_clock::now();
        
        
        cout << " Searching elements in Set2 time - " << chrono::duration_cast<chrono::nanoseconds>(etime - stime ).count() << " nano sec \n";


        stime = chrono::steady_clock::now();
        for( int i = 1 ; i <= SEARCH_NUM ; i++ )
                auto it = uset1.find(i); 
        etime = chrono::steady_clock::now();
        
        
        cout << " Searching elements in Unordered Set1 time - " << chrono::duration_cast<chrono::nanoseconds>(etime - stime ).count() << " nano sec \n";


        stime = chrono::steady_clock::now();
        for( int i = 1 ; i <= SEARCH_NUM ; i++ )
                auto it = uset2.find(i); 
        etime = chrono::steady_clock::now();
        
        
        cout << " Searching elements in Unordered Set2 time - " << chrono::duration_cast<chrono::nanoseconds>(etime - stime ).count() << " nano sec \n";

        cout <<"\n";
        
}
        

######################output ########################


Related Solutions

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) create a vector of 100,000 elements of integers, with the values initially set as 1~100,000, in this order. randomize the ordering of these...
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...
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...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet will be a simple class consisting of three fields: name: string representing the name of a planet, such as “Mars” madeOf: string representing the main element of the planet alienPopulation: int representing if the number of aliens living on the planet Your Planet class should have the following methods: Planet(name, madeOf, alienPopulation) // Constructor getName(): string // Returns a planet’s name getMadeOf(): String //...
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.
In C++ write an implementation of the ADT sorted list that uses a resizable array (vector...
In C++ write an implementation of the ADT sorted list that uses a resizable array (vector class of C++ STL) to represent the list items.
C++ language. Suppose you are given a list of students registered in a course and you...
C++ language. Suppose you are given a list of students registered in a course and you are required to implement an Linked List of student. Each student in the list has name, regNo and cGpa. You task is to implement following function related to Linked List. insertStudent() : This function, when called will prompt user to enter his/her choice of insertion. The options are 1. Insert student at the start of linked list 2. Insert student at the end of...
Language C: Suppose you are given a file containing a list of names and phone numbers...
Language C: Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
Assume you have the following set of job evaluation factors: A. Educational Requirements B. Experience C....
Assume you have the following set of job evaluation factors: A. Educational Requirements B. Experience C. Responsibility D. Mental Effort E. Physical Effort F. Working Conditions G. Work Leadership A B C D E F G Admin. Secretary I 4 5 3 2 1 1 1 Technician II 4 2 1 2 1 2 1 Engineer I 5 1 2 3 2 2 1 Engineer IV 6 4 4 5 1 1 3 Accounting Leader 5 3 5 4 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT