❖ Performance |
Why do we care about performance?
Good performance → less hardware, happy users.
Bad performance → more hardware, unhappy users.
Generally: performance = execution time
Other measures: memory/disk space, network traffic, disk i/o.
Execution time can be measured in two ways:
❖ ... Performance |
In the (distant) past, performance was a significant problem
❖ Development Strategy |
A pragmatic approach to efficiency:
❖ ... Development Strategy |
Strategy for developing efficient programs:
❖ Performance Analysis |
Complexity/estimates give some idea of performance in advance.
Often, however ..
Performance analysis can be:
❖ ... Performance Analysis |
Coarse-grained performance analysis
time
$ time ./myProg < LargeInput > /dev/null real 0m5.064s user 0m4.113s sys 0m0.802s
❖ ... Performance Analysis |
Components of Unix time
real
user
sys
❖ ... Performance Analysis |
Using time
user
sys
user+sys
real
Note also: some versions of time
user
sys
❖ ... Performance Analysis |
Decades of empirical study of program execution have shown ...
The 90/10 rule generally holds (or 80/20 rule or ...):
Implications:
❖ Profiles |
Need a method for locating hot spots
An execution profile for a program is
❖ ... Profiles |
A profile shows how much time is spent in each code block ...
Software tools can generate profiles of program execution.
❖ gprof |
The gprof
-pg
gmon.out
gprof
gmon.out
Example of use:
$ gcc -pg -o xyz xyz.c $ xyz < data > /dev/null $ gprof xyz | less
For further usage details, man gprof
❖ ... gprof |
The gprof
It gives a flat profile containing:
It also gives a call graph, containing:
❖ Profile Example |
Consider the following program ...
// search for words in text containing a given substring // display each such word once (in alphabetical order) int main(int argc, char*argv[]) { char word[MAXWORD]; // current word List matches; // list of matched words char *substring; // string to look for FILE *input; // the input file // ... Check command-line args, open input file ... // Process the file - find the matching words matches = newList(); while (getWord(input, word) != NULL) { if (contains(word,substring) && !member(matches,word)) matches = insert(matches,word); } printWords(matches); return 0; }
❖ ... Profile Example |
Flat profile for this program (xwords et data3
% cumulative self self total time seconds seconds calls us/call us/call name 75.00 0.03 0.03 30212 0.99 0.99 getWord 25.00 0.04 0.01 30211 0.33 0.33 contains 0.00 0.04 0.00 489 0.00 0.00 member 0.00 0.04 0.00 267 0.00 0.00 insert 0.00 0.04 0.00 1 0.00 40000.00 main 0.00 0.04 0.00 1 0.00 0.00 printWords
The flat profile shows which functions contribute most to the execution cost.
For more info on how to interpret gprof
Note: wc data3
7439 30211 188259
❖ ... Profile Example |
% cumulative self self total time seconds seconds calls us/call us/call name 75.00 0.03 0.03 30212 0.99 0.99 getWord 25.00 0.04 0.01 30211 0.33 0.33 contains 0.00 0.04 0.00 489 0.00 0.00 member 0.00 0.04 0.00 267 0.00 0.00 insert 0.00 0.04 0.00 1 0.00 40000.00 main 0.00 0.04 0.00 1 0.00 0.00 printWords
We can learn quite a lot from this:
member()
getword()
❖ ... Profile Example |
Call graph for the same execution (xwords et data3
index %time self children called name 0.00 0.04 1/1 _start [2] [1] 100.0 0.00 0.04 1 main [1] 0.03 0.00 30212/30212 getWord [3] 0.01 0.00 30211/30211 contains [4] 0.00 0.00 489/489 member [5] 0.00 0.00 267/267 insert [6] 0.00 0.00 1/1 printWords [7] ----------------------------------------------- [2] 100.0 0.00 0.04 _start [2] 0.00 0.04 1/1 main [1] ----------------------------------------------- 0.03 0.00 30212/30212 main [1] [3] 75.0 0.03 0.00 30212 getWord [3] ----------------------------------------------- 0.01 0.00 30211/30211 main [1] [4] 25.0 0.01 0.00 30211 contains [4] ---------------------------------------------- 0.00 0.00 489/489 main [1] [5] 0.0 0.00 0.00 489 member [5] ----------------------------------------------- 0.00 0.00 267/267 main [1] [6] 0.0 0.00 0.00 267 insert [6] ----------------------------------------------- 0.00 0.00 1/1 main [1] [7] 0.0 0.00 0.00 1 printWords [7]
❖ ... Profile Example |
What does each entry mean?
index %time self children called name 0.00 0.04 1/1 _start [2] [1] 100.0 0.00 0.04 1 main [1] 0.03 0.00 30212/30212 getWord [3] 0.01 0.00 30211/30211 contains [4] 0.00 0.00 489/489 member [5] 0.00 0.00 267/267 insert [6] 0.00 0.00 1/1 printWords [7]
This entry shows info about the main()
main()
_start
main()
main()
getWord
contains()
main()
member()
main()
❖ ... Profile Example |
The above call graph is unusual
main()
For more info on how to interpret gprof
Produced: 11 Jul 2020