Performance Metrics and Decorator Patterns
Performance Metrics and Decorator Patterns
Performance Metrics and Decorator Patterns
Performance Metrics
and
Decorator Patterns
Design Patterns for Data Structures
• 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
Parametric decoration
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
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;
}
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
≤ > ≤ >
23 6
8 6
Design Patterns for Data Structures Figure 4.29
Height = 3
>
2 3
8
8 8
Design Patterns for Data Structures
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)