C Profiling with gprof 🎯

beginner
5 min

C Profiling with gprof 🎯

Welcome to the exciting world of C Profiling with gprof! Today, we're going to dive into this powerful tool that helps us understand the performance of our C programs. Let's get started!

What is Profiling? 📝

Profiling is the process of measuring the execution time, function calls, and memory usage of a program to find bottlenecks and optimize its performance. In this lesson, we'll focus on using gprof to profile C programs.

Installing gprof ✅

Before we start, make sure you have gprof installed. It's a part of the GNU Compiler Collection (GCC), so if you have GCC installed, you already have gprof. If not, install GCC and you're good to go!

Writing a Profiled Program 💡

Let's write a simple C program and profile it with gprof. Here's our first example:

c
/* main.c */ #include <stdio.h> void func_a(int n) { int i; for (i = 0; i < n; i++) printf("Hello, World!\n"); } void func_b(int n) { int i, j; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) printf("Hello, World!\n"); } } int main() { int n = 10000; func_a(n); func_b(n); return 0; }

This program defines two functions (func_a and func_b) that print "Hello, World!" and a main function that calls these functions with a large number (n = 10000).

Profiling the Program 💡

To profile our program, compile it with the -g (generate debugging info) and -pg (profile with gprof) flags:

bash
gcc -g -o main main.c -pg

Now, run the compiled program:

bash
./main

After running the program, we generate the profile report with the gprof command:

bash
gprof ./main

The output will be a detailed report about the execution of our program.

Understanding the gprof Report 📝

The gprof report consists of several sections:

  • Program name: The name of the compiled program.
  • List of functions: A list of functions in the order of their execution.
  • % time: The total execution time of each function as a percentage.
  • Self time: The actual execution time of each function.
  • Total time: The total execution time of each function.

Let's analyze the report for our example:

Each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls seconds seconds name 67.45 0.67 0.67 10000 0.67 0.67 main 32.55 1.05 0.38 10000 0.38 1.05 func_b 0.00 1.05 0.00 1 0.00 1.05 func_a

From the report, we can see that our main function takes 67.45% of the total execution time, while func_b takes 32.55%. This indicates that func_b might be the bottleneck in our program.

Optimizing the Program 💡

With gprof's help, we can now focus on optimizing the bottleneck function. In our case, we can optimize func_b by reducing the nested loop:

c
void func_b_optimized(int n) { int i, j, total = n * n; for (i = 0; i < total; i++) printf("Hello, World!\n"); }

Now, repeat the profiling process with the optimized version of the program and compare the results.

Quiz 📝

Quick Quiz
Question 1 of 1

What does profiling help us with?

That's it for today! In the next lesson, we'll dive deeper into gprof and learn more advanced techniques for profiling C programs. Happy coding! 😊