Home
menubar
rss_feed

Previous Reference C++ Reference <ctime> Next Reference




C++ Reference: clock()





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:








Previous Reference C++ Reference <ctime> Next Reference




Home | Reference | Play Games! | Blog | Forum | Site Map | Contact Us


shadow bottom image