Performance Measurement and Improvement
time prog -n 17
- A Trigram Game
- Practical Hints
Measurement and Profiling
Computers operate on two fundamentally different time scales.
At a microscopic level
To execute instructions at a rate of one or more per clock cycle, where each clock cycle requires only around one nanosecond (abbreviated “ns”)
On a macroscopic scale
processor must respond to external events that occur on time scales measured in milliseconds (abbreviated “ms”).
Process Scheduling and Timer Interrupts
[简答题] interval time
Measuring Time by Interval Counting
[简答题] time segment
- The operating system also uses the timer to recordthe cumulative time used by each process.
- This information provides a somewhat imprecisemeasure of program execution time.
- A time segment refers to the period during whichjust one process executes.
time prog -n 17
[简答题] time mechanisms
The 80/20 Rule
- 80% of the CPU time is spent in 20% of the program
- These places where the computer spends most of its time are also called hot spots,inner loops, and kernels.
- All programs have hot spots.
- A little effort can have large payoffs.
- If only part of a program can be sped up with parallel processing, then thesequential part of the program woulddetermine and limit the possible speed up.
- Knowing how to optimize is an importantskill. Knowing when to stop optimizing isalso important.
P34 A Trigram Game？？？
Some Expensive Operations
printf()causes malloc to be called in the usual implementation, adding an unexpected cost
- String manipulation is expensive, whether it is formatting text as in printf, reading ASCII text andconverting to numbers, or performing stringcomparisons.
- Modern computers are designed to accessmemory a word at a time. Accessing a character is no faster than accessing a word, and may be slower.
- String operations usually operate one character ata time, i.e strlen(), has to examine every character,one at a time.
- use static or local variables to avoid allocating memory on the heap.
- keep a list of objects that need to be allocated often => explicit free-list.
- Then, allocation is just a matter of removing an object from the list, and freeing simply inserts the object on the list.
Fast Allocation and Free