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

Lec 03

This document discusses performance analysis and measurement of algorithms. It notes that performance can be analyzed using paper and pencil without a computer by examining asymptotic complexity, but that analysis does not account for constant factors or memory hierarchy effects. It states that accurate performance measurement requires implementing the algorithm in a programming language, running it on an actual computer, and using a timing mechanism like clock() to measure elapsed time over many repetitions to improve accuracy. Issues like initialization, clock resolution, and variability in timing measurements are also discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views19 pages

Lec 03

This document discusses performance analysis and measurement of algorithms. It notes that performance can be analyzed using paper and pencil without a computer by examining asymptotic complexity, but that analysis does not account for constant factors or memory hierarchy effects. It states that accurate performance measurement requires implementing the algorithm in a programming language, running it on an actual computer, and using a timing mechanism like clock() to measure elapsed time over many repetitions to improve accuracy. Issues like initialization, clock resolution, and variability in timing measurements are also discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, 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

Measure 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