skip to Main Content

I have to compare the time differentiation between bubble sort, selection sort, linear search, and binary search functions. I have already completed all of my functions, but every time I display execution time, it displays a 0. Anyway around this, or do I have to change something about my formatting… My time display starts at the end of case 1. I did not include my other functions into this example.

using namespace std;
using std::time;
using namespace std::chrono;

int Menu(); // Menu for sorting and search options.
void bubbleSort(int [], int); // Applies bubble sort algorithm to sort the elements of an unsorted array
void selectionSort(int [], int); // Applies selection sort algorithm to sort the elements of an unsorted array
int linearSearch(int [], double[], int, int ); // Applies the linear search algorithm to search for book ID
int binarySearch(int [], double [], int, int); // Applies the binary search algorithm to search for book ID
void display(string [], int [], double [], int); // To display the contents of parallel array in a tabular format



int main()
{

    int size = 10, choice, searchKey, searchKey2, result, result2, quantity=0, quantity2=0;
    const int SIZE = 1000; // Constant max for number of numbers random number generator will output
    const int MAXRANGE = 500; // Constant range for the range of numbers random number generator will sort through
    int selectionsort[ SIZE ] = {0};
    int bubblesort[ SIZE ] = {0};
    time_t t;
    srand((unsigned) time(&t));


    string bookTitle[] = {"Starting out with C++", "Java Programming", "Software Structures",
    "Design and Analysis of Algorithms", "Computer Graphics", "Artificial Intelligence: A Modern Approach",
    "Probability and Statistics", "Cognitive Science", "Modern Information Retrieval", "Speech and Language Processing"};   // Parallel Arrays

    int bookID[] = {1101, 1211, 1333, 1456, 1567, 1642, 1699, 1755, 1800, 1999};

    double bookPrice[] = {112.32, 73.25, 54.00, 67.32, 135.00, 173.22, // Use of parallel arrays to match data with book ID and price
    120.00, 42.25, 32.11, 123.75};



    while (choice != 5) // Loops as long as user does not enter 5
    {
        choice = Menu();
        if (choice == 5){
         cout << "Thanks for stopping by! Please, do come again soon :)";   break;} // Program terminates if user enters 5
        switch (choice)
        {
        case 1:
            display(bookTitle, bookID, bookPrice, size); // Displays book title, ID, and price using arrays
            cout << "***LINEAR SEARCH***" << endl;
            cout << "Please enter a book ID to purchase: ";
            cin >> searchKey;
            result = linearSearch(bookID, bookPrice, size, searchKey);
            if (result >=0){ // Searches in the array for value user enters, this way it will find the price of each book.

                cout << "The book " << bookTitle[result] << " was chosen." << endl;
                cout << "Please enter how many you would like to purchase: ";
            result = linearSearch(bookID, bookPrice, size, searchKey);
                cin >> quantity; // Quantity used in order to count number of books using the array the user wishes to purchase.
                cout << "You have purchased " << quantity << " " << bookTitle[result] << endl << "For the total price of $" << bookPrice[result] * quantity << endl;
                cout << "Thank you for your business!" << endl << endl;
                high_resolution_clock::time_point t1 = high_resolution_clock::now();
                linearSearch(bookID, bookPrice, size, searchKey);
                high_resolution_clock::time_point t2 = high_resolution_clock::now();

                auto duration = duration_cast<microseconds>( t2 - t1 ).count();

                cout << "The time the process took is " << duration << " milliseconds" << endl << endl;
            }

2

Answers


  1. There’s a great example here: http://en.cppreference.com/w/cpp/chrono/high_resolution_clock/now

    #include <iostream>
    #include <vector>
    #include <numeric>
    #include <chrono>
    
    volatile int sink;
    int main()
    {
        for (auto size = 1ull; size < 1000000000ull; size *= 100) {
            // record start time
            auto start = std::chrono::high_resolution_clock::now();
            // do some work
            std::vector<int> v(size, 42);
            sink = std::accumulate(v.begin(), v.end(), 0u); // make sure it's a side effect
            // record end time
            auto end = std::chrono::high_resolution_clock::now();
            std::chrono::duration<double> diff = end-start;
            std::cout << "Time to fill and iterate a vector of " 
                      << size << " ints : " << diff.count() << " sn";
        }
    }
    
    Login or Signup to reply.
  2. #include <iostream>
    #include <chrono>
    
    using namespace std;
    using namespace std::chrono;
    
    void function()
    {
        long long number = 0;
    
        for( long long i = 0; i != 2000000; ++i )
        {
           number += 5;
        }
    }
    
    int main()
    {
        high_resolution_clock::time_point t1 = high_resolution_clock::now();
        function();
        high_resolution_clock::time_point t2 = high_resolution_clock::now();
    
        auto duration = duration_cast<microseconds>( t2 - t1 ).count();
    
        cout << duration;
        return 0;
    }
    

    Here is some example code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search