gprof を触りだけ
gprofとは
プロファイラ.
参考
https://www.howtoforge.com/tutorial/how-to-install-and-use-profiling-tool-gprof/
環境
paiza cloud.
~$ g++ --version g++ (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609 Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ~$ gprof --version GNU gprof (GNU Binutils for Ubuntu) 2.26.1 Based on BSD gprof, copyright 1983 Regents of the University of California. This program is free software. This program has absolutely no warranty.
(bash on ubuntu) は関数がうまく拾えないのか,何も表示されませんでした.(バージョン違いかも?)
.
install
apt install binutils
use
適当なプログラムがあったとする.hoge.cpp
.
#include <bits/stdc++.h> using namespace std; int func1(){ int x = 0; for (int i = 0; i < 10101010; ++i){ x += i; x %= 107; } return x; } int func2(){ int x = 0; for (int i = 0; i < 40101010; ++i){ x *= i; x %= 107; } return x; } int func3(){ int y = 0; for (int i = 0; i < 4; ++i){ y += func1(); } return y; } int main(){ func1(); func2(); func3(); return 0; }
-pg
をつける
g++ hoge.cpp -pg
-pg
を付けてコンパイルしたファイルを実行すると,gmon.out
を生成する.
a.out
とgmon.out
をgprof
に渡す.
~$ gprof a.out gmon.out Flat profile: Each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls ms/call ms/call name 56.36 0.43 0.43 5 86.79 86.79 func1() 44.56 0.78 0.34 1 343.12 343.12 func2() 0.00 0.78 0.00 1 0.00 0.00 _GLOBAL__sub_I__Z5func1v 0.00 0.78 0.00 1 0.00 0.00 __static_initialization_and_destruction_0(int, int) 0.00 0.78 0.00 1 0.00 347.16 func3() 以下続く
関数単位で表示されるので,例えば
#include <bits/stdc++.h> using namespace std; int main(){ int x = 0; for (int i = 0; i < 10101010; ++i) x += i; x %= 107; for (int i = 0; i < 40101010; ++i) x *= i; x %= 107; for (int j = 0; j < 4; ++j) for (int i = 0; i < 10101010; ++i) x += i; x %= 107; return 0; }
なコードを与えると,
% cumulative self self total time seconds seconds calls Ts/call Ts/call name 95.90 0.29 0.29 main 0.00 0.29 0.00 1 0.00 0.00 _GLOBAL__sub_I_main 0.00 0.29 0.00 1 0.00 0.00 __static_initialization_and_destruction_0(int, int)
はい.
-O3
-O2
を付けても行方不明になることがある.
~$ g++ hoge.cpp -pg -O3 ~$ ./a.out 10 0 40 ~$ gprof a.out gmon.out | head -n 20 Flat profile: Each sample counts as 0.01 seconds. no time accumulated % cumulative self self total time seconds seconds calls Ts/call Ts/call name 0.00 0.00 0.00 1 0.00 0.00 _GLOBAL__sub_I__Z5func1v % the percentage of the total running time of the time program used by this function. cumulative a running sum of the number of seconds accounted seconds for by this function and those listed above it. self the number of seconds accounted for by this seconds function alone. This is the major sort for this listing. calls the number of times this function was invoked, if
気持ち
使い慣れない…