What is the difference between <chrono> and <ctime>?
To measure the execution time of a function, I can use both parameters. But what is the difference between using <chrono> and <ctime> ? Should I prefer one over the other?
ctime is a C-style header, it's an old but not safe type, and not as accurate as chrono . chrono is the preferred option in C ++; it is a modern C ++ header, it is type-safe, just like our equipment allows, it has extended functionality and, more importantly, it follows C ++ (not C) logic, so some things will be more natural / expressive with him and so we can expect him to know about many modern language functions (threads, exceptions, etc.) - we cannot make the same assumptions for ctime .
However, there are several more use ctime for ctime (or even time.h ), for example. when we need to talk with some APIs or when we rely on old code bases or when we use a library that follows different logic. C ++ is designed to be pragmatic and not to be "clean" in every way; therefore, ctime and all kinds of obsolete headers, syntaxes, and language functions still exist, even if programmers are not recommended to use them.
ctime is an old school. Its only use now is a solution for getting dates from time points (since C ++ does not have an adequate standard datetime library). For general time needs, use chrono . If you need to include system_clock::time_point in a date / time, use ctime .