std::difftime(3)
π NAME π
okay so bestie, welcome to std::difftime(3) from the C++ Standard Libary manual β yes, βLibary,β the page said it, not me π. The NAME section is literally:
std::difftime β std::difftime
no extra lore, just the function being its own icon π .
β¨ Synopsis β¨
Itβs defined in header <ctime> and the signature is exactly:
double difftime( std::time_t time_end, std::time_t time_beg );
It 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 β yes, it can go backwards, bestie π.
π Parameters π
time_beg,time_endβ times to compare
πΈ Return value πΈ
Difference between two times in seconds. Thatβs the whole vibe β¨.
π¦ 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. Thatβs the nuance, iykyk π.
π Example π
// Run this code π
#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.
π See also π
durationβ a time interval(C++11)(class template)- C documentation for
difftime
mwah π this page comes from http://cppreference.com, dated 2024.06.10, and its page id is std::difftime(3) β periodt. π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms