0

Since C++11 we can measure time as in https://en.cppreference.com/w/cpp/chrono

#include <iostream>
#include <chrono>
 
long fibonacci(unsigned n)
{
    if (n < 2) return n;
    return fibonacci(n-1) + fibonacci(n-2);
}
 
int main()
{
    auto start = std::chrono::steady_clock::now();
    std::cout << "f(42) = " << fibonacci(42) << '\n';
    auto end = std::chrono::steady_clock::now();
    std::chrono::duration<double> elapsed_seconds = end-start;
    std::cout << "elapsed time: " << elapsed_seconds.count() << "s\n";
}

How people used to measure time before chrono? Was there a C++ way to do it or people used to reply on OS specific facilities?

KcFnMi
  • 5,516
  • 10
  • 62
  • 136
  • 2
    `time.h` and `ctime`? Reading the number of clock cycles? – Rogue Feb 07 '23 at 16:30
  • 3
    On Windows I used to use GetTickCount function, and also QueryPerformanceCounter/QueryPerformanceFrequency for more accurate measurments. – wohlstad Feb 07 '23 at 16:32
  • On embedded systems, we would hook up an oscilliscope to a test point. We would assert the test point before the measurement and deassert after. Actually, we still use this technique. :-) Recently (in terms of decades), we would use LEDs as test points or connect them to the test point. This gives a visual representation of the timing. – Thomas Matthews Feb 07 '23 at 16:38
  • 2
    There's almost always an OS function in C that, in many cases, predates even C++. – tadman Feb 07 '23 at 16:40
  • On embedded systems, we would have a 1ms Interrupt Service Routine (ISR). We would have a "tick" counter. Reading the tick counter before and after would be used for event timing. For longer times, we would read the Real Time Clock (RTC). – Thomas Matthews Feb 07 '23 at 16:42
  • Occasionally you'd have a custom timing rig with an equally custom API. Sometimes, and it's getting less common as time goes by, the chrono implementation is dodgy and you have to go old school. – user4581301 Feb 07 '23 at 16:42
  • 5
    Before , computers just couldn't tell the time. There was no clock in the bottom right corner of the screen, and programmers had to get out the stopwatch to do performance measurements. – user253751 Feb 07 '23 at 16:42
  • 1
    I still don't use std::chrono for my timings (at least not in this sense, I do use chrono a lot for time calculations) but I use profilers to get data on how much time is spent in what part of my code so I don't mix production code with tests. And chrono in the end measures wall time so when the code is not scheduled to run time still passes. I want to know how much cpu cycles a bit of code consumed. And more often then not I found a bottleneck in parts where I didn't think it would be. – Pepijn Kramer Feb 07 '23 at 16:47
  • https://stackoverflow.com/questions/2808398/easily-measure-elapsed-time top answers are chrono, others are no-chrono – 463035818_is_not_an_ai Feb 07 '23 at 18:09
  • @PepjinKramer I agree with the general advice, but OP is just asking for measuring time. Also not always measureing time is about finding performance bottlenecks, sometimes something needs to happen within a certain fixed time x, and nobody cares if it takes 1ms or 1ns as long as it takes less than x wall time – 463035818_is_not_an_ai Feb 07 '23 at 18:14
  • Since when do we have , was it introduced with C++11? – KcFnMi Feb 08 '23 at 15:34
  • 1
    C++11 introduced chrono, mainly to meet the needs of the also introduced multithreading API. C++14 added constexpr and literals to chrono. C++17 added round, ceil and floor to augment time_point_cast and duration_cast. C++20 added calendar and time zone support, more clocks, formatting and parsing. – Howard Hinnant Feb 08 '23 at 15:54

0 Answers0