Namespaces
Variants
Views
Actions

std::difftime

From cppreference.com
< cpp‎ | chrono‎ | c
Revision as of 10:59, 19 December 2012 by Cubbi (Talk | contribs)

Defined in header <ctime>
double difftime( time_t time2, time_t time1 );

Computes difference between two calendar times as Template:rlpt objects (time2 - time1) in seconds.

Contents

Parameters

time1, time2 - times to compare

Return value

Difference between two times in seconds

Notes

On POSIX systems, std::time_t is measured in seconds, and difftime is equivalent to arithmetic subtraction, but C++ (and C) allows fractional units for time_t.

Example

#include <iostream>
#include <ctime>
int main()
{
    std::time_t start = std::time(NULL);
    volatile double d;
    for(int n=0; n<10000; ++n)
       for(int m=0; m<100000; ++m)
           d += d*n*m; // some time-consuming operation
    std::cout << "Wall time passed: "
              << std::difftime(std::time(NULL), start) << " s.\n";
}

Output:

Wall time passed: 7 s.

See also

Template:cpp/chrono/dcl list duration
C documentation for difftime