Profiling example
/* mpfact.c
*
* multiple precision factorial computation
*
* example for performance analysis of programs
*
* TODO: add basic operations mpz_*
*
* (c) GPL due to using GMP
*
*/
#include <gmp.h> /* use GMP */
#include <stdio.h>
#include <stdlib.h>
#define MAXN 100000L
int main(int argc, char *argv[]) {
int i;
long int n;
mpz_t x;
if(argc!=2) {
fprintf(stderr,"Usage: %s number\n", argv[0]);
return 1;
}
n = strtol(argv[1],NULL,10);
if(n < 0 || n > MAXN) {
fprintf(stderr,"%s: number out of range (0..%ld)\n", argv[0], MAXN);
return 1;
}
mpz_init_set_ui(x,1);
for(i=1; i<=n; i++)
mpz_mul_ui(x,x,i);
printf("\n%ld! = ", n);
mpz_out_str(stdout,10,x);
printf("\n\n");
return 0;
}
Output
time ./mpfact 5000 GMP
real 0m0.021s
user 0m0.012s
sys 0m0.004s
time ./mpfact- 5000 fgmp
real 0m7.262s
user 0m6.252s
sys 0m0.028s
gprof output (using program linked with fgmp - simple GMP-like library):
Flat profile:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls s/call s/call name
78.98 12.29 12.29 16326 0.00 0.00 udiv
12.34 14.21 1.92 5000 0.00 0.00 mpz_mul
3.98 14.83 0.62 16326 0.00 0.00 urshift
1.61 15.08 0.25 32652 0.00 0.00 ulshift
1.09 15.25 0.17 142421 0.00 0.00 _mpz_realloc
0.90 15.39 0.14 86631 0.00 0.00 mpz_set
0.90 15.53 0.14 70313 0.00 0.00 digits
0.19 15.56 0.03 126090 0.00 0.00 mpz_clear
0.00 15.56 0.00 121092 0.00 0.00 mpz_init
0.00 15.56 0.00 32654 0.00 0.00 uzero
0.00 15.56 0.00 16326 0.00 0.00 zero
0.00 15.56 0.00 5001 0.00 0.00 mpz_init_set_ui
0.00 15.56 0.00 5000 0.00 0.00 mpz_mul_ui
0.00 15.56 0.00 2 0.00 0.00 mpz_sizeinbase
0.00 15.56 0.00 1 0.00 13.62 mpz_get_str
0.00 15.56 0.00 1 0.00 13.62 mpz_out_str
0.00 15.56 0.00 1 0.00 0.00 mpz_set_ui
% 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
this function is profiled, else blank.
self the average number of milliseconds spent in this
ms/call function per call, if this function is profiled,
else blank.
total the average number of milliseconds spent in this
ms/call function and its descendents per call, if this
function is profiled, else blank.
name the name of the function. This is the minor sort
for this listing. The index shows the location of
the function in the gprof listing. If the index is
in parenthesis it shows where it would appear in
the gprof listing if it were to be printed.
Call graph (explanation follows)
granularity: each sample hit covers 4 byte(s) for 0.06% of 15.56 seconds
index % time self children called name
[1] 100.0 0.00 15.56 main [1]
0.00 13.62 1/1 mpz_out_str [2]
0.00 1.94 5000/5000 mpz_mul_ui [5]
0.00 0.00 1/5001 mpz_init_set_ui [17]
-----------------------------------------------
0.00 13.62 1/1 main [1]
[2] 87.5 0.00 13.62 1 mpz_out_str [2]
0.00 13.62 1/1 mpz_get_str [3]
0.00 0.00 1/2 mpz_sizeinbase [13]
-----------------------------------------------
0.00 13.62 1/1 mpz_out_str [2]
[3] 87.5 0.00 13.62 1 mpz_get_str [3]
12.29 1.33 16326/16326 udiv [4]
0.00 0.00 1/86631 mpz_set [9]
0.00 0.00 1/2 mpz_sizeinbase [13]
0.00 0.00 2/126090 mpz_clear [12]
0.00 0.00 16328/32654 uzero [15]
0.00 0.00 4/121092 mpz_init [14]
0.00 0.00 1/1 mpz_set_ui [18]
-----------------------------------------------
12.29 1.33 16326/16326 mpz_get_str [3]
[4] 87.5 12.29 1.33 16326 udiv [4]
0.62 0.07 16326/16326 urshift [7]
0.25 0.15 32652/32652 ulshift [8]
0.05 0.05 32652/86631 mpz_set [9]
0.07 0.00 32652/70313 digits [11]
0.06 0.00 48978/142421 _mpz_realloc [10]
0.02 0.00 65304/126090 mpz_clear [12]
0.00 0.00 65304/121092 mpz_init [14]
0.00 0.00 16326/32654 uzero [15]
0.00 0.00 16326/16326 zero [16]
-----------------------------------------------
0.00 1.94 5000/5000 main [1]
[5] 12.5 0.00 1.94 5000 mpz_mul_ui [5]
1.92 0.02 5000/5000 mpz_mul [6]
0.00 0.00 5000/126090 mpz_clear [12]
0.00 0.00 5000/5001 mpz_init_set_ui [17]
-----------------------------------------------
1.92 0.02 5000/5000 mpz_mul_ui [5]
[6] 12.5 1.92 0.02 5000 mpz_mul [6]
0.01 0.01 5000/86631 mpz_set [9]
0.01 0.00 5000/142421 _mpz_realloc [10]
0.00 0.00 5000/126090 mpz_clear [12]
0.00 0.00 5000/121092 mpz_init [14]
-----------------------------------------------
0.62 0.07 16326/16326 udiv [4]
[7] 4.5 0.62 0.07 16326 urshift [7]
0.03 0.02 16326/86631 mpz_set [9]
0.02 0.00 16326/142421 _mpz_realloc [10]
0.00 0.00 16326/126090 mpz_clear [12]
0.00 0.00 16326/121092 mpz_init [14]
-----------------------------------------------
0.25 0.15 32652/32652 udiv [4]
[8] 2.5 0.25 0.15 32652 ulshift [8]
0.05 0.05 32652/86631 mpz_set [9]
0.04 0.00 32652/142421 _mpz_realloc [10]
0.01 0.00 32652/126090 mpz_clear [12]
0.00 0.00 32652/121092 mpz_init [14]
-----------------------------------------------
0.00 0.00 1/86631 mpz_get_str [3]
0.01 0.01 5000/86631 mpz_mul [6]
0.03 0.02 16326/86631 urshift [7]
0.05 0.05 32652/86631 ulshift [8]
0.05 0.05 32652/86631 udiv [4]
[9] 1.7 0.14 0.12 86631 mpz_set [9]
0.07 0.00 37659/70313 digits [11]
0.05 0.00 39465/142421 _mpz_realloc [10]
0.00 0.00 1806/126090 mpz_clear [12]
0.00 0.00 1806/121092 mpz_init [14]
-----------------------------------------------
0.01 0.00 5000/142421 mpz_mul [6]
0.02 0.00 16326/142421 urshift [7]
0.04 0.00 32652/142421 ulshift [8]
0.05 0.00 39465/142421 mpz_set [9]
0.06 0.00 48978/142421 udiv [4]
[10] 1.1 0.17 0.00 142421 _mpz_realloc [10]
-----------------------------------------------
0.00 0.00 2/70313 mpz_sizeinbase [13]
0.07 0.00 32652/70313 udiv [4]
0.07 0.00 37659/70313 mpz_set [9]
[11] 0.9 0.14 0.00 70313 digits [11]
-----------------------------------------------
0.00 0.00 2/126090 mpz_get_str [3]
0.00 0.00 1806/126090 mpz_set [9]
0.00 0.00 5000/126090 mpz_mul [6]
0.00 0.00 5000/126090 mpz_mul_ui [5]
0.00 0.00 16326/126090 urshift [7]
0.01 0.00 32652/126090 ulshift [8]
0.02 0.00 65304/126090 udiv [4]
[12] 0.2 0.03 0.00 126090 mpz_clear [12]
-----------------------------------------------
0.00 0.00 1/2 mpz_get_str [3]
0.00 0.00 1/2 mpz_out_str [2]
[13] 0.0 0.00 0.00 2 mpz_sizeinbase [13]
0.00 0.00 2/70313 digits [11]
-----------------------------------------------
0.00 0.00 4/121092 mpz_get_str [3]
0.00 0.00 1806/121092 mpz_set [9]
0.00 0.00 5000/121092 mpz_mul [6]
0.00 0.00 16326/121092 urshift [7]
0.00 0.00 32652/121092 ulshift [8]
0.00 0.00 65304/121092 udiv [4]
[14] 0.0 0.00 0.00 121092 mpz_init [14]
-----------------------------------------------
0.00 0.00 16326/32654 udiv [4]
0.00 0.00 16328/32654 mpz_get_str [3]
[15] 0.0 0.00 0.00 32654 uzero [15]
-----------------------------------------------
0.00 0.00 16326/16326 udiv [4]
[16] 0.0 0.00 0.00 16326 zero [16]
-----------------------------------------------
0.00 0.00 1/5001 main [1]
0.00 0.00 5000/5001 mpz_mul_ui [5]
[17] 0.0 0.00 0.00 5001 mpz_init_set_ui [17]
-----------------------------------------------
0.00 0.00 1/1 mpz_get_str [3]
[18] 0.0 0.00 0.00 1 mpz_set_ui [18]
-----------------------------------------------
This table describes the call tree of the program, and was sorted by
the total amount of time spent in each function and its children.
Each entry in this table consists of several lines. The line with the
index number at the left hand margin lists the current function.
The lines above it list the functions that called this function,
and the lines below it list the functions this one called.
This line lists:
index A unique number given to each element of the table.
Index numbers are sorted numerically.
The index number is printed next to every function name so
it is easier to look up where the function in the table.
% time This is the percentage of the `total' time that was spent
in this function and its children. Note that due to
different viewpoints, functions excluded by options, etc,
these numbers will NOT add up to 100%.
self This is the total amount of time spent in this function.
children This is the total amount of time propagated into this
function by its children.
called This is the number of times the function was called.
If the function called itself recursively, the number
only includes non-recursive calls, and is followed by
a `+' and the number of recursive calls.
name The name of the current function. The index number is
printed after it. If the function is a member of a
cycle, the cycle number is printed between the
function's name and the index number.
For the function's parents, the fields have the following meanings:
self This is the amount of time that was propagated directly
from the function into this parent.
children This is the amount of time that was propagated from
the function's children into this parent.
called This is the number of times this parent called the
function `/' the total number of times the function
was called. Recursive calls to the function are not
included in the number after the `/'.
name This is the name of the parent. The parent's index
number is printed after it. If the parent is a
member of a cycle, the cycle number is printed between
the name and the index number.
If the parents of the function cannot be determined, the word
`' is printed in the `name' field, and all the other
fields are blank.
For the function's children, the fields have the following meanings:
self This is the amount of time that was propagated directly
from the child into the function.
children This is the amount of time that was propagated from the
child's children to the function.
called This is the number of times the function called
this child `/' the total number of times the child
was called. Recursive calls by the child are not
listed in the number after the `/'.
name This is the name of the child. The child's index
number is printed after it. If the child is a
member of a cycle, the cycle number is printed
between the name and the index number.
If there are any cycles (circles) in the call graph, there is an
entry for the cycle-as-a-whole. This entry shows who called the
cycle (as parents) and the members of the cycle (as children.)
The `+' recursive calls entry shows the number of function calls that
were internal to the cycle, and the calls entry for each member shows,
for that member, how many times it was called from other members of
the cycle.
Index by function name
[10] _mpz_realloc [6] mpz_mul [4] udiv (gmp.c)
[11] digits (gmp.c) [5] mpz_mul_ui [8] ulshift (gmp.c)
[12] mpz_clear [2] mpz_out_str [7] urshift (gmp.c)
[3] mpz_get_str [9] mpz_set [15] uzero (gmp.c)
[14] mpz_init [18] mpz_set_ui [16] zero (gmp.c)
[17] mpz_init_set_ui [13] mpz_sizeinbase