Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

Update to Linux perf report

Improved handling of inlined frames


#include <vector>
#include <complex>
#include <random>
#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

namespace {
vector<double> generateVector(size_t entries)
{
    uniform_real_distribution<double> uniform(-1E5, 1E5);
    default_random_engine engine;
    vector<double> vec;
    for (size_t i = 0; i < entries; ++i) {
        vec.push_back(norm(complex<double>(uniform(engine), uniform(engine))));
    }
    return vec;
}
}

int main()
{
    auto vec = generateVector(5000000);
    std::sort(vec.begin(), vec.end());
    std::cout << "median is:" << vec[vec.size() / 2] << '\n';
    return 0;
}

$ g++ -O2 -g inline.cpp -o inline
$ perf record --call-graph dwarf ./inline
median is:6.36103e+09
[ perf record: Woken up 24 times to write data ]
[ perf record: Captured and wrote 5.819 MB perf.data (721 samples) ]

$ perf report
...
-   97.36%    11.37%  inline   inline            [.] main
   - 85.99% main
      + 56.17% std::__introsort_loop<__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, long, __gnu_cxx::__ops::_Iter_less_iter>
      + 15.53% hypot
        12.21% std::generate_canonical<double, 53ul, std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul> >
      + 1.53% std::vector<double, std::allocator<double> >::_M_realloc_insert<double>
   + 10.54% _start
   + 0.83% 0x7ffcb296f33f
...

$ perf report --inline
-   97.36%    11.37%  inline   inline            [.] main
   + 85.99% main
      main (inline)
      generateVector (inline)
      std::uniform_real_distribution<double>::operator()<std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul> > (inline)
      std::uniform_real_distribution<double>::operator()<std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul> > (inline)
      std::__detail::_Adaptor<std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul>, double>::operator() (inline)

$ perf report
-   97.36%    11.37%  inline   inline            [.] main
   - 85.99% main
      - 56.17% std::sort<__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > > > (inlined)
           std::__sort<__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__ops::_Iter_less_iter> (inlined)
         + std::__introsort_loop<__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, long, __gnu_cxx::__ops::_Iter_less_iter>
      - 29.82% generateVector (inlined)
         + 16.09% std::norm<double> (inlined)
         + 12.21% std::uniform_real_distribution<double>::operator()<std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul> > (inlined)
         + 1.53% std::vector<double, std::allocator<double> >::push_back (inlined)

$ perf report -s sym -g srcline
-   97.36%    11.37%  [.] main
   - 56.17% main inline.cpp:26
        std::sort<__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > > > stl_algo.h:4836 (inlined)
      + std::__sort<__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__ops::_Iter_less_iter> stl_algo.h:1968 (inlined)
   - 29.82% main inline.cpp:25
      - generateVector inline.cpp:17 (inlined)
         - 16.09% std::norm<double> complex:664 (inlined)
              std::_Norm_helper<true>::_S_do_it<double> complex:654 (inlined)
              std::abs<double> complex:597 (inlined)
            - std::__complex_abs complex:589 (inlined)
               + 15.53% hypot

MilianWolff

Milian Wolff

Senior Software Engineer

Learn Modern C++

Learn more