Namespaces
Variants
Views
Actions

std::difftime

From cppreference.com
< cpp‎ | chrono‎ | c
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (C++20)
Dynamic memory management
Program utilities
Coroutine support (C++20)
Variadic functions
Debugging support
(C++26)
Three-way comparison
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)

 
 
C-style date and time utilities
Functions
Time manipulation
difftime
Format conversions
Constants
Types
(C++17)
 
Defined in header <ctime>
double difftime( std::time_t time_end, std::time_t time_beg );

Computes difference between two calendar times as std::time_t objects (time_end - time_beg) in seconds. If time_end refers to time point before time_beg then the result is negative.

Contents

[edit] Parameters

time_beg, time_end - times to compare

[edit] Return value

Difference between two times in seconds.

[edit] Notes

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

[edit] Example

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

Possible output:

Wall time passed: 9 s.

[edit] See also

(C++11)
a time interval
(class template) [edit]
C documentation for difftime