The standard GNU profiler, gprof, is available for most Unix-like systems. gprof is part of the GNU toolchain and was one of the earliest open source code-profiling tools. It combines compile-time instrumentation and sampling techniques, using a 100 Hz sample rate. It has the advantage that it does not require kernel support.
To prepare a program for profiling with gprof, you add -pg to the compile and link flags, which injects code that collects information about the call tree into the function preamble. When you run the program, samples are collected and stored in a buffer, which is written to a file named gmon.out when the program terminates.
if you encounter below error while running the gprof command:
you may try installing the binutils package as show below as per your choice of distribution.
|OS X||brew install binutils|
|Debian||apt-get install binutils|
|Ubuntu||apt-get install binutils|
|Alpine||apk add binutils|
|Arch Linux||pacman -S binutils|
|Kali Linux||apt-get install binutils|
|CentOS||yum install binutils|
|Fedora||dnf install binutils|
|Raspbian||apt-get install binutils|
gprof displays the profile data for an object file. The file’s symbol table is compared with the call graph profile file gmon.out (previously created by compiling with gcc -pg). Many of gprof’s options take a symbol-specification argument, or symspec, to limit the option to specified files or functions. The symspec may be a filename, a function, or a line number. It can also be given as filename:function or filename:linenumber to specify a function or line number in a specific file. gprof expects filenames to contain a period and functions to not contain a period.