# Chapter 8: Profilers

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.

— Donald Knuth, JON POSTEL, Structured Programming with go to Statements

## What is a profiler?

According to wikipedia, profiling is is the investigation of a program's behavior using information gathered as the program runs (i.e. it is a form of dynamic program analysis, as opposed to static code analysis). The usual goal of performance analysis is to determine which parts of a program to optimize for speed or memory usage.

A logback profiler help the developer gather performance data by managing one or more stopwatches driven by statements inserted in the source code. An example, should make the point clearer.

doSomethingBasic() { Profiler profiler = new Profiler("BASIC"); profiler.start("Subtask_1"); doSubtaskOne(); profiler.start("Subtask_1"); for (int i = 0; i < 5; i++) { doSubtaskTwo(i); } profiler.start("Other"); doOther(); profiler.stop().print();

+ Profiler [BASIC] |-- elapsed time [Subtask_1] 9.987 milliseconds. |-- elapsed time [Subtask_1] 47.263 milliseconds. |-- elapsed time [Other] 2.790 milliseconds. |-- Total elapsed time [BASIC] 60.092 milliseconds.