Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Created January 20, 2023 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaityo256/2530281a9c49d433ecbff8a33195d415 to your computer and use it in GitHub Desktop.
Save kaityo256/2530281a9c49d433ecbff8a33195d415 to your computer and use it in GitHub Desktop.
gprof sample
#include <cstdio>
double func(double a){
for (int i=0;i<100000000;i++){
a = a * 3.0;
if (a > 1.0) a -= 1.0;
if (a > 1.0) a -= 1.0;
}
return a;
}
double func2(double a){
for (int i=0;i<100000000;i++){
a = a * 2.5;
if (a > 1.0) a -= 1.0;
if (a > 1.0) a -= 1.0;
}
return a;
}
int main(){
double a = func(0.1);
double b = func2(0.1);
printf("%f %f\n",a,b);
}
@kaityo256
Copy link
Author

-pgオプションをつけてコンパイルする。

g++ -pg test.cpp

実行する。

$ ./a.out
0.275867 0.379889

gmon.outというファイルができている。

$ ls
a.out*  gmon.out  test.cpp

この状態でgprofを実行する。

$ gprof
Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls   s/call   s/call  name
 56.33      1.15     1.15        1     1.15     1.15  func(double)
 44.96      2.08     0.92        1     0.92     0.92  func2(double)
(snip)

どの関数が何回呼ばれ、一度あたり何秒かかり、全体でどのくらいの割合を占めているかが表示される。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment