|
424 | 424 | \end{cppcode*}
|
425 | 425 | \end{exampleblock}
|
426 | 426 | \end{frame}
|
| 427 | + |
| 428 | +\begin{frame}[fragile] |
| 429 | + \frametitle{\cpp date and time utilities \hfill \cpp11 / \cpp20} |
| 430 | + \cppinline{std::chrono library} present in \cppinline{<chrono>} header |
| 431 | + \begin{block}{Clocks} |
| 432 | + \begin{itemize} |
| 433 | + \item consists of a starting point (or epoch) and a tick rate |
| 434 | + \begin{itemize} |
| 435 | + \item E.g. January 1, 1970 and every second |
| 436 | + \end{itemize} |
| 437 | + \item C++ defines several clock type |
| 438 | + \begin{itemize} |
| 439 | + \item \href{https://en.cppreference.com/w/cpp/chrono/system_clock}{\color{blue!50!white} \cppinline{system_clock}} system time, aka wall clock time, or C time |
| 440 | + \item \href{https://en.cppreference.com/w/cpp/chrono/steady_clock}{\color{blue!50!white} \cppinline{steady_clock}} monotonic but unrelated to wall clock time |
| 441 | + \item \href{https://en.cppreference.com/w/cpp/chrono/high_resolution_clock}{\color{blue!50!white} \cppinline{high_resolution_clock}} clock with the smallest tick period |
| 442 | + \end{itemize} |
| 443 | + \end{itemize} |
| 444 | + \end{block} |
| 445 | + \begin{block}{\href{https://en.cppreference.com/w/cpp/chrono/time_point}{\color{blue!50!white} \cppinline{time_point}} and \href{https://en.cppreference.com/w/cpp/chrono/duration}{\color{blue!50!white} \cppinline{duration}}} |
| 446 | + \begin{itemize} |
| 447 | + \item provide easy manipulation of times and duration |
| 448 | + \item clock dependent |
| 449 | + \item \href{https://en.cppreference.com/w/cpp/chrono/duration/duration_cast}{\color{blue!50!white} \cppinline{duration_cast}} allows conversions between duration types |
| 450 | + \begin{itemize} |
| 451 | + \item available helper types : nanoseconds, microseconds, milliseconds, seconds, minutes, hours, ... |
| 452 | + \end{itemize} |
| 453 | + \end{itemize} |
| 454 | + \end{block} |
| 455 | +\end{frame} |
| 456 | + |
| 457 | +\begin{frame}[fragile] |
| 458 | + \frametitlecpp[11]{Practical usage / timing some \cpp code} |
| 459 | + \begin{exampleblockGB}{How to measure the time spent in some code}{https://godbolt.org/z/PzKWer5eb}{\texttt{timing}} |
| 460 | + \small |
| 461 | + \begin{cppcode*}{gobble=2} |
| 462 | + #include <chrono> |
| 463 | + |
| 464 | + std::chrono::high_resolution_clock clock; |
| 465 | + clock::time_point start = clock::now(); |
| 466 | + ... // code to be timed |
| 467 | + std::chrono::duration<float> ticks = clock::now() - start; |
| 468 | + |
| 469 | + auto millis = std::chrono::duration_cast |
| 470 | + <std::chrono::milliseconds>(duration); |
| 471 | + std::cout << "it took " << duration.count() << " ticks" |
| 472 | + << ", that is " << millis.count() << " ms\n"; |
| 473 | + \end{cppcode*} |
| 474 | + \end{exampleblockGB} |
| 475 | + \pause |
| 476 | + \begin{alertblock}{Warning} |
| 477 | + \begin{itemize} |
| 478 | + \item this does not measure the amount of CPU used ! |
| 479 | + \item neither the time spend on a CPU (think suspended threads) |
| 480 | + \end{itemize} |
| 481 | + \end{alertblock} |
| 482 | +\end{frame} |
0 commit comments