Week9-Lecture22-24
Lecture 22 Composition¶
Lab 07 Linked list, Inheritance¶
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
Lecture 23 Decomposition¶
Disc 08 Linked list¶
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
Lecture 24 Efficiency¶
Reading 2.8 Efficiency¶
2.8.1 Measuring efficiency¶
更可靠的描述程序效率的方法是测量某个事件发生的次数
另一方面,我们还有知道函数调用占用的空间
2.8.2 Memorization¶
记忆化函数会存储它之前接收过的任何参数对应的返回值。
| Python | |
|---|---|
2.8.3 Orders of Functions¶
我们很难精确描述一个函数需要调用的次数和占用的空间,因此我们使用增长阶描述。我们只关注它怎么增长(增长的数量级)
R(n) 可以衡量所使用的内存量、执行的基本机器步骤数
Θ 表示法。设 n 是衡量某个过程输入规模的参数,并设 R(n) 是该过程处理规模为 n 的输入时所需的某种资源的量。
2.8.4 Example: Exponentiation¶
2.8.5 Growth Categories¶
了解并识别常见增长阶是很重要的。
- 常数项不影响过程的增长量级。
- 对数的底不影响过程的增长量级。
- 嵌套。当一个内层计算过程在外层过程的每一步中重复时,整个过程的增长量级是外层过程与内层过程步数的乘积。
| Python | |
|---|---|
- 在一个求和中,除增长最快的项外,其他所有项都可以省略,而不会改变增长阶。
| Category | Theta Notation | Growth Description | Example |
|---|---|---|---|
| Constant | Θ(1) | Growth is independent of the input | abs |
| Logarithmic | Θ(logn) | Multiplying input increments resources | fast_exp |
| Linear | Θ(n) | Incrementing input increments resources | exp |
| Quadratic | Θ(n2) | Incrementing input adds n resources | one_more |
| Exponential | Θ(bn) | Incrementing input multiplies resources | fib |