0% found this document useful (0 votes)
6 views19 pages

Lec 003

The document discusses performance measurement and analysis, highlighting the importance of evaluating algorithms without the need for a computer. It outlines the limitations of performance analysis, particularly regarding constant factors and memory hierarchy, and emphasizes the need for accurate timing mechanisms in programming. Additionally, it provides examples of timing methods in C and addresses common pitfalls in measuring execution time.

Uploaded by

dkchangmin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views19 pages

Lec 003

The document discusses performance measurement and analysis, highlighting the importance of evaluating algorithms without the need for a computer. It outlines the limitations of performance analysis, particularly regarding constant factors and memory hierarchy, and emphasizes the need for accurate timing mechanisms in programming. Additionally, it provides examples of timing methods in C and addresses common pitfalls in measuring execution time.

Uploaded by

dkchangmin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Performance Measurement

Performance Analysis -

Paper and pencil.

Don’t need a working computer program or even a computer.


Some Uses Of Performance Analysis -

➢determine practicality of algorithm


➢predict run time on large instance
➢compare 2 algorithms that have different asymptotic complexity
➢e.g., O(n) and O(n2)
Limitations of Analysis -

Doesn’t account for constant factors.

but constant factor may dominate


1000n vs n2
and we are interested only in
n < 1000
Limitations of Analysis -

Modern computers have a hierarchical memory


organization with different access time for memory at
different levels of the hierarchy.
Memory Hierarchy -

MAIN
L2
L1
ALU R
8-32 32KB 512KB 512MB
1C 2C 10C 100C
Limitations of Analysis -

Our analysis doesn’t account for this difference in memory access


times.

Programs that do more work may take less time than those that do
less work.
Performance Measurement -

actual time on an actual computer


.

What do we need?
Performance Measurement Needs -

programming language
working program
computer
compiler and options to use




Performance Measurement Needs -
data to use for measurement
worst-case data
best-case data
average-case data

timing mechanism --- clock




Timing In C
clock_t start, stop;

start = clock(); /* set start to current time in


hundredths of a second */

/* code to be timed comes here */

stop = clock(); /* set stop to current time */

runTime = (double) (stop – start)/


CLOCKS_PER_SEC;
Shortcoming
Clock accuracy
assume 100 ticks

Repeat work many times to bring total


time to be >= 1000 ticks
Accurate Timing
start = clock();
counter = 0;
do {
counter++;
doSomething();
} while (clock() - start < 1000)

elapsedTime = (double) (clock() – start)/


CLOCKS_PER_SEC;
timeForTask = elapsedTime/counter;
Accuracy
Now accuracy is 10%.

first reading may be just about to change to start


+ 100

second reading may have just changed to stop

so stop - start is off by 100 ticks


Accuracy

first reading may have just changed to


start

second reading may be about to change


to stop + 100

so stop - start is off by 100 ticks


Accuracy

Examining remaining cases, we get

trueElapsedTime = stop - start +- 100

To ensure 10% accuracy, require

elapsedTime = stop – start


>= 1000
What Went Wrong?
start = clock();
counter = 0;
do {
counter++;
insertionSort(a,n);
} while (clock() - start < 1000)
elapsedTime = (double)(clock() – start)/
CLOCKS_PER_SEC;
timeToSort = elapsedTime/counter;
The Fix
start = clock();
counter = 0;
do {
counter++;
/* put code to initialize a here */
insertionSort(a,n);
} while (clock() - start < 1000)
Bad Way To Time
do {
counter++;
start = clock();
doSomething();
stop = clock();
elapsedTime += stop - start;
} while (elapsedTime < 1000)

You might also like