Code analysis#

Code analysis

Content

Introduction#

Analyzing code could done by many ways. Two of them are profiling and benchmarking.

The concept of profiling is to inspect a code and see how and where it takes time (or memory usage or both). The idea is to return a deterministic result (for equal situation of entries/stimuli) to allow the developer to optimize his code or find problems.

Important

It is important to know that an execution profile is not a benchmark as the profiler adds some overhead!

Then, a benchmark may be seen as a simple measure of total elapsed time for an overall operation.

Profiling vs benchmarking#

As said, be aware that profiling is not the same as benchmarking. A profiling result is typically some kind of array with methods, timings and memory usage. This is aimed to tell “this program is taking many resources in this method”. And then an optimization in this area may lead to great results.

A benchmark is typically a single time measure (or many if many situations are tested) aimed to compare two configurations (typically gaming graphic cards comparisons) or two implementations of a method (for example computing a complex math operation using two different algorithms).

The Python case#

As a popular language, Python is given many tools with many features.

Profilers#

Most profilers are using the cProfile (and profile which is a different implementation of the same profiling interface) as engine and offers graphical output and/or advanced analysis.

The documentation is a great base.

Be aware of some limitations. First the precision, as many profiler rely on a millisecond-level clock or the overhead that’s added.

More advanced tools are:

  • SnakeViz which give a nice graphical output

Benchmarks#

The most used library to measure time is timeit. It’s really easy to use and there is many ways of using it. The documentation is just really clear and concise.

profiling benchmark snakeviz timeit Python