I think that all those duration_cast<duration<double>> will use a lot of cpu power. |
Do not judge the dog by its fur.
There are two considerations:
* Correctness. We can write code so complicated that tracing bugs is a nightmare. A bit simpler implementation, even if it uses a bit more expensive tools is "cheaper" to maintain. Correct code is more important than quick undefined behaviour. While there are many characters in chrono-incantations (and other standard library/latest C++ bits) , they are still quite simple, precise, and to the point (and provides ... well whatever resolution the underlying hardware/OS has). The <ctime> & co. are C-based older alternatives and some of them are officially deprecated.
* Compiler optimizations. Do you know how the standard library has been implemented? Do you know what optimization steps the compiler can do to such code during compilation? The compiler might be able to evaluate some expressions already during compilation so that the binary won't have many instructions to execute during runtime.
If we write something "simple", like
auto
in
integralfx's example, it may not be so simple in reality. On the other hand the long-looking
std::chrono::duration_cast<std::chrono::milliseconds>
could be a simple "multiply with constant".
@
SakurasouBusters:
Your solutions are not standard C++. They are specific to some operating system and will not work everywhere.
You have no idea where
integralfx did run the example, nor how loaded that system was.