Performance Metrics and Decorator Patterns

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Design Patterns for Data Structures Chapter 4

Performance Metrics
and
Decorator Patterns
Design Patterns for Data Structures

Theoretical asymptotic bounds of sort algorithms


mance Metrics and Decorator Patterns

• Merge sort — Θ (n lg n)
• Worst case quick sort — Θ (n2 )
• Average case quick sort — Θ (n lg n)
• Selection sort — Θ (n2 )
• Insertion sort — Θ (n2 )
• Heap sort — Θ (n lg n)
These bounds are all based on theoretical stateme
execution counts experimentally
Design Patterns for Data Structures without relying on the s
sign, a decoration is a functionality that is added to an
some desired side effect.
While it would be difficult to experimentally count the
How can
in a sorting we measure
program, the performance?
a decorator pattern can count the ex
statements. The execution count is the side effect the pat
causeNeed a metric:
the pattern a standard
does of measurement.
not provide a total count of the nu
sort, the count it does provide is a relative indication of the
A metric is a standard of measurement. This section d
Three possible
the following performance metrics
metrics.
• The number of array element comparisons.
• The number of array element assignments.
• The number of array element probes.
An example of an element comparison is in the compariso
! !
Design Patterns
! for Data Structures
! One element comparison

Two element probes

One element assignment


sons would increase the count by one for each execution of the if guard
Design Patterns for Data Structures
o count the number of probes would increase the count by two for each
he if guard, once for the access to a[i] and once for the access to a[
example of an element assignment is the assignment statement
a[mid] = a[j];
Use decorator patterns to collect data
n the split() method of quick sort. A decorator to count the number o
using these three metrics
would increase the count by one for each execution of the assignment stat
This chapter describes two decorators that collect data using the above
• Parametric decoration — for comparison and assignment metrics.
• Decorator design pattern — for the probe metric.
Both decorator patterns use C++ templates in conjunction with function
Strictly speaking, parametric decoration is not an OO design pattern as i
nheritance. Implementation of the probe metric uses the OO decorator de
Design Patterns for Data Structures

Demo SortCompAsgn Project


Design Patterns for Data Structures Figure 4.30
Design Patterns for Data Structures Figure 4.30
Design Patterns for Data Structures

Parametric decoration

• Do not sort type double.


• Instead, sort object with class CAMetrics.
• In CAMetrics set up two counters, one to count the
number of comparisons and one to count the
number of assignments.
• Overload the comparison operators to increment
the comparison counter when executed.
• Overload the assignment operator to increment
the assignment counter when executed.
Design Patterns for Data Structures Figure 4.31
static Counter _assignCount;
Design Patterns for Data Structures Figure 4.31
T _data;

public:
CAMetrics(); // for array initialization.
CAMetrics(CAMetrics const &rhs);
T toT() const;
void setT(const T &source);
bool operator<(CAMetrics const &rhs);
bool operator<(CAMetrics const &rhs) const;
bool operator<=(CAMetrics const &rhs);
bool operator<=(CAMetrics const &rhs) const;
bool operator==(CAMetrics const &rhs);
bool operator==(CAMetrics const &rhs) const;
bool operator!=(CAMetrics const &rhs);
bool operator!=(CAMetrics const &rhs) const;
bool operator>=(CAMetrics const &rhs);
bool operator>=(CAMetrics const &rhs) const;
bool operator>(CAMetrics const &rhs);
bool operator>(CAMetrics const &rhs) const;
CAMetrics &operator=(CAMetrics const &rhs);
Design Patterns for Data Structures Figure 4.31
Design Patterns for Data Structures Figure 4.32
Design Patterns for Data Structures Figure 4.32
Design Patterns for Data Structures Figure 4.33

154 Chapter 4 Comparison S


// ========= Comparison operators =========
template<class T>
bool CAMetrics<T>::operator<(CAMetrics<T> const &rhs) {
_compareCount.update();
return _data < rhs._data;
}

template<class T>
bool CAMetrics<T>::operator<(CAMetrics<T> const &rhs) const {
_compareCount.update();
return _data < rhs._data;
}

template<class T>
bool CAMetrics<T>::operator<=(CAMetrics<T> const &rhs) {
_compareCount.update();
return _data <= rhs._data;
template<class T>
boolDesign Patterns for Data Structures const &rhs)
CAMetrics<T>::operator<(CAMetrics<T> Figureconst
4.33{
_compareCount.update();
return _data < rhs._data;
}

template<class T>
bool CAMetrics<T>::operator<=(CAMetrics<T> const &rhs) {
_compareCount.update();
return _data <= rhs._data;
}

template<class T>
bool CAMetrics<T>::operator<=(CAMetrics<T> const &rhs) const {
_compareCount.update();
return _data <= rhs._data;
}

Figure 4.33 CAMetrics.hpp (continued). Implementation of t


loaded comparison operators for the CAMetrics class. The other f
similar and are not shown. The program listing continues in the next
Design Patterns for Data Structures Figure 4.34
Design Patterns for Data Structures

A Comparison Sort Bound

Any sort of n elements that is based on element


comparison and exchange is (n lg n).
Design Patterns for Data Structures
n!
The proof uses the following lemma.

n! ≥ (n/2)n/2

n!
= ⟨ n! n ⟩
n · (n − 1) · (n − 2) · . . . · ( n2 + 1) · ( n2 ) · . . . · 3 · 2 · 1
= ⟨ a · b = b · a⟩
1n · 2(n − 1) · 3(n − 2) · . . . · ( 2n )( n2 + 1)
≥ ⟨ n/2 n/2⟩
(n/2)n/2

n
Design Patterns for Data Structures Figure 4.29

a:b
≤ >
b:c a:c Height = 3
≤ > ≤ >
a:c b:c
≤ > ≤ >

(a)!Maximum decisions = 3. Number of leaves = 6. (b)!M

23 6
8 6
Design Patterns for Data Structures Figure 4.29

Height = 3

>

6. (b)!Maximum decisions = 3. Number of leaves = 8.

2 3
8
8 8
Design Patterns for Data Structures

There are n! leaves in the decision tree


to sort n elements.

A decision tree of height h has at most


2h leaves.

2 h
n!
Design Patterns for Data Structures

2h ≥ n!
= ⟨ ⟩
h ≥ lg(n!)
⇒ ⟨ n! ≥ (n/2)n/2 lg⟩
h ≥ lg(n/2)n/2
= ⟨ lg ab = b lg a⟩
h ≥ (n/2) lg(n/2)
= ⟨h = ⟩
≥ (n/2) lg(n/2)
= ⟨ Ω⟩
= Ω ((n/2) lg(n/2))
= ⟨ Ω ⟩
= Ω (n lg n)

You might also like