如何使用<chrono>来查看函数执行的时间

时间:2016-05-02 03:35:12

标签: c++ chrono

我必须比较冒泡排序,选择排序,线性搜索和二元搜索功能之间的时间差异。我已经完成了所有的功能,但每次显示执行时间时,它都显示为0.无论如何,或者我必须更改有关格式化的内容......我的时间显示从案例1结束开始。我没有将我的其他功能包含在这个例子中。

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 个答案:

答案 0 :(得分:0)

这里有一个很好的例子: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() << " s\n";
    }
}

答案 1 :(得分:0)

#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;
}

以下是一些示例代码。