std::difftime(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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