Because "time" is bad and intuition is worst: Header_only timer/chrono for benchmark/optimization purpose
Often I find myself confronted with questions relating to code optimization.
What I've ultimately learned about those question is that my, or anybody, intuition about how the compiler and the CPU would behave regarding a given line of code is irremediably BAD ⛔.
So my final answer [that will come after some generals advises as: (complexity, data structure) >> micro optimization)]
is that nothing is certain nor true before measuring ⚖️.
That being said, many people (at 42 school) don't know how to easily measure their time performance,
and will often end up doing the time ./a.out
poop 💩.
⭐ The purpose of this repo is to address that issue the simple way ⭐.
- Clone the repo and add
chronos.h
to yours library.
exemple:
git clone https://github.com/LesChats/chronos.h.git chronos && cp chronos/chronos.h includes/ && rm -rf chronos
- Include
chronos.h
and add thet_chronos
structure where you need to measure.
- Surround the 🔥 part of your code whit the
start_chronos
andend_chronos
functions
then display the elapsed time wherever you please usingget_diff
exemple 1:
exemple 2:
- Finaly execute your program and take note of the result in your terminal
-
Result elapsed time will differ at each launch of the executable because of the computer “noise”
so it is recommended to launch it (and take note of the results) several times. -
The most meaningful result is the minimum elapsed time you find (not the max, not the mean, not even the median).
-
Do not take account of very small differences, they mostly noise.
-
You can re-use the functions and the same structure anywhere in your code sevral times.