Performance Counters

Learn about performance monitoring and testing, as well as the perf tool for best practices.

Performance monitoring

Apart from the obvious properties, such as execution time and memory usage, it can sometimes be beneficial to measure other things. Either because they are more reliable or because they can give us better insights into what is causing our code to run slow.

Many CPUs are equipped with hardware performance counters that can provide us with metrics such as the number of instructions, CPU cycles, branch mispredictions, and cache misses. We haven't introduced these hardware aspects yet in this course, and we will not explore performance counters in depth.

However, it's good to know that they exist and that there are ready-made tools and libraries (accessible through APIs) for all the major operating systems to collect Performance Monitoring Counters (PMC) while running a program.

The support for performance counters varies depending on the CPU and operating system. Intel provides a powerful tool called VTune, which can be used for monitoring performance counters. FreeBSD offers pmcstat. macOS comes with DTrace and Xcode Instruments. Microsoft Visual Studio provides support for collecting CPU counters on Windows.

Another popular tool is perf, which is available on GNU/Linux systems. Running the command:

Get hands-on with 1200+ tech skills courses.