C Standard Libraries C++

clock()

Declaration

clock_t clock();

Description

This function returns the number of ticks passed since the start of the process. The number of ticks is equal to the number of seconds times CLOCKS_PER_SEC.

Example

#include <iostream>
#include <ctime>

int main() {
    using namespace std;

    clock_t qStartTime;
    clock_t qEndTime;

    const unsigned int kuiNumberCount = 20000000;
    const unsigned int kuiModulus = 6;

    // Time the random number generator
    qStartTime = clock();
    for (unsigned int uiIndex = 0; uiIndex < kuiNumberCount; ++uiIndex) {
        unsigned int uiInteger = rand();
        unsigned int uiRand = (uiInteger % kuiModulus);
    }
    qEndTime = clock();

    double dElapsedSecs = (double)(qEndTime - qStartTime)/CLOCKS_PER_SEC;
    cout << "Generating " << kuiNumberCount
        << " random numbers between 0 and "
        << (kuiModulus - 1) << " takes "
        << dElapsedSecs << " seconds." << endl;

    return 0;
}

Output

clock() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.