Integer Partition 219
time seconds seconds calls s / call s/ call name
55.74 6.75 6.75 1 6.75 11.99 p a rtiti o n2
43.28 11.99 5.24 53687 0912 0.00 0.00 isValid
1.50 12.17 0.18 1 0.18 0.18 partit ion1
0.00 12.17 0.00 20 2786 0.00 0.00 print P artition
index % time self chi ldren ca lled name
< spontaneous >
[1] 100.0 0.00 12.17 main [1]
6.75 5.24 1/1 par t ition2 [2]
0.18 0.00 1/1 par t ition1 [4]
-------- - - - - - - - - --------------- - - - - - - - - - -------
107 3 741823 par t ition2 [2]
6.75 5.24 1/1 main [1]
[2] 98.5 6.75 5.24 1 + 1073741 8 23 parti tion2 [2]
5.24 0.00 536870912 / 5 3 6 870912 isValid [3]
0.00 0.00 1 0 1393/202 7 86 prin t Partition [5]
107 3 741823 par t ition2 [2]
-------- - - - - - - - - --------------- - - - - - - - - - -------
5.24 0.00 536870912 / 5 3 6 870912 p artiti on2 [2]
[3] 43.1 5.24 0.00 53687 0912 isV alid [3]
-------- - - - - - - - - --------------- - - - - - - - - - -------
308288 pa r tition 1 [4]
0.18 0.00 1/1 main [1]
[4] 1.5 0.18 0.00 1+308288 partit ion1 [4]
0.00 0.00 1 0 1393/202 7 86 prin t Partition [5]
308288 pa r tition 1 [4]
-------- - - - - - - - - --------------- - - - - - - - - - -------
0.00 0.00 1 0 1393/202 7 86 par tition 1 [4]
0.00 0.00 1 0 1393/202 7 86 par tition 2 [2]
[5] 0.0 0.00 0.00 2 02786 printPart i tion [5]
-------- - - - - - - - - --------------- - - - - - - - - - -------
This report shows that nearly 99% of the program’s execution time is taken by
partition2 (55.7%) and isValid (43.3%). Since isValid is called only by partition2, the
report shows that partition2 takes 98.5% of the time. In contrast, partition1 takes only
1.5% of the time. Why is there such a large difference? The reason is that partition2 gen-
erates many invalid partitions and then uses isValid to check before printing. In contrast,
partition1 generates only valid partitions. The latter approach reduces huge portions of
the call tree, and is thus much more efficient.
Please notice that [4] shows printPartition is called by partition1 101393 times
and by partition2 101393 times. This is expected since partition1 and partition2
should print exactly the same partitions. The function partition2 is called 1073741823
times by recursion but partition1 is called only 308288 times. The report also shows that
isValid is called 536870912 times but printPartition is called only 202786 times (by
both partition1 and partition2). This means that most of the generated partitions are
invalid and are not printed. This is another indication that partition2 generates many
invalid partitions. Notice that 55.74%, 43.28%, and 1.50% sum to 100.52%. How can it be
possible that the cumulative time is above 100%? This shows a limitation of gprof: This
tool is intended to be fast but the accuracy is not so great. Profiling code always interferes
with the normal execution of the code, and there must be a trade-off somewhere.
When we want to improve a program’s performance, then we first need to identify
the functions that take the most amount of time. We can get some help by using gprof.