sirius
sirius

Reputation: 607

macOS Time Profiler profiling c++ code but can't find my function name

I wrote a simple test c++ code as my first trying with Time Profiler in my mac:

#include <iostream>     // std::cout

int frank() { return 0; }

int main () {
  int a = frank();
  std::cout << a << std::endl;
  return 0;
}

Then I compiled it:

g++ test.cpp -o test0

And I use Time Profile to profile my executable file test0:

enter image description here

I thought I could find my function frank() in Call Tree, but I didn't.

Another question is how to pass arguments to an executable file in Time Profiler.

Upvotes: 4

Views: 2214

Answers (1)

danglingpointer
danglingpointer

Reputation: 712

Instruments is a sampling profiler for which the sample frequency is too low to detect your call to frank(). Either perform some computation-expensive calculation inside the function or simply call it a sufficient number of times.

Adjusting your code to the following (C++11) where a slightly more complex function is called within a loop:

#include <iostream>     // std::cout

long frank(const unsigned int n) { return n*n; }

int main () {
    const auto nr_iterations = 1000000u;
    long a;
    for (auto n = 0u; n < nr_iterations; ++n)
    {
        a = frank(n);
    }
    std::cout << a << std::endl;
    return 0;
}

Results in the following result in Instruments on my machine:

enter image description here

To pass arguments via Instruments, click on the target name right next to the record button. In the context menu that appears, click 'Edit target’ where ‘target’ is your executable's name. For example:

enter image description here

A dialogue window appears where you can input the arguments.

Upvotes: 2

Related Questions