2

Given the following code that either calls f or g

#include <stdlib.h>
#include <stdio.h>

int f() {
    return 0;
}

int g() {
    return 1;
}

int main() {
    long sum = 0;
    for(int i = 0; i < 1000*1000*1000; i++) {
        int result;
        if(rand() % 2 == 0) {
            result = f();
        }
        else {
            result = g();
        }

        sum += result;
    }
    printf("%ld\n", sum);
}

I compile with

g++ test.c -o doom -lprofiler -lunwind

And run with

CPUPROFILE=./test.txt ./test

And then generate a gif with

pprof --gif ./test ./test.txt > output.gif

I get the following enter image description here

However, I have edges going from f to g, and f to itself, and g to itself. There are no optimizations on (and I tried again with -O0 to be sure), and I've also tried with and without -lunwind.

Why does the profiler think that f calls g (and itself somtimes)? How do I use the profiler correctly?

archgoon
  • 1,538
  • 2
  • 14
  • 19

0 Answers0