<style> .red { color: red; font-weight: bold; } .blue { color: blue; font-weight: bold; } .green { color: green; font-weight: bold; } </style> # 第一次期中考重點 ## Chap 1 ### Performance :::success <font class="red">效能是執行時間的倒數</font> $$ \text{Performance}_x\ =\ \frac{1}{\text{Execution time}_x} $$ :::info **Example**: Computer A runs a program in 10 sec, computer B runs the **same program** in 15 sec. **How much faster is A than B?** **Ans.** $$ \rm\frac{Performance_A}{Performance_B}\ =\ \frac{Execution\ time_B}{Execution\ time_A}\ =\ \frac{15}{10}\ =\ 1.5 $$ So, **A is 1.5 times faster than B**, or **B is 1.5 times slower than A**. ::: :::success - **Clock cycle** (tick, clock tick, clock period, clock, cycle) - The time for **one clock period** (ps) - **Clock rate** - The **inverse** of the clock period (Hz) ::: ### CPU performance and its factors :::warning - **CPU execution time** for a program = <font class="blue">**CPU clock cycles for a program $\times$ Clock cycle time**</font> = <font class="blue">**CPU clock cycles for a program / Clock rate**</font> :::info **Example**: Improving performance **Q.** - A program runs in 10 seconds on computer A, which has a 2 GHz clock. - We want to build a computer B that will run this program in 6 seconds. - To increase the clock rate is possible, but will require 1.2 times as many clock cycles as computer A. - **What clock rate should computer B have?** **A.** 1. $$ \begin{split} \rm{CPU\ Time_A} &= \rm{CPU\ Clock\ Cycles_A} / {Clock\ Rate_A} \\ 10 &= \rm{CPU\ Clock\ Cycles_A} / (2 \times 10^9) \\ \rm CPU\ Clock\ &\rm Cycles_A = 20 \times 10^9 Cycles \end{split} $$ 2. $$ \begin{split} \rm{CPU\ Time_B} &= 1.2 \times \rm{CPU\ Clock \ Cycles_A} / {Clock\ Rate_B} \\ 6 &= 1.2\times (20 \times 10^9) / \rm{Clock\ Rate_B} \\ &\rm{Clock\ Rate_B} = 4GHz \end{split} $$ - ***To run the program in 6 seconds, B must have twice the clock rate of A***. ::: ### Instruction performance The execution time must depend on the number of instructions in a program: :::success - **CPU clock cycles required for a program** = <font class="blue">**Instructions for a program $\times$ Average clock cycles per instruction**</font> - **Clock cycles per instruction (CPI)** = <font class="blue">**The <font style="background-color: lightblue">average</font> number of clock cycles each instruction takes to execute**</font> :::info **Example**: Using the performance equation **Q.** - **Computer A and B have the same ISA** - Computer A: clock cycle time 250 ps, CPI 2.0 for **same** program - Computer B: clock cycle time 500 ps, CPI 1.2 for the **same** program - Which computer is faster for this program and by how much? **A.** - Assume the program has $I$ instruction - CPU clock cycles A = $I$ $\times$ 2.0, CPU clock cycles B = $I$ $\times$ 1.2 - CPU timeA = $I$ $\times$ 2.0 $\times$ 250 = 500 $\times$ $I$ ps **faster !** CPU timeB = $I$ $\times$ 1.2 $\times$ 500 = 600 $\times$ $I$ ps - ***Computer A is 1.2 times as fast as computer B for this program*** ::: ### The classic CPU performance equation :::warning - Instruction count - The number of instructions executed by the program - **CPU time** = <font class="blue">**Instruction count $\times$ CPI $\times$ Clock cycle time**</font> = <font class="blue">**Instruction count $\times$ CPI / Clock rate**</font> :::info **Example**: Comparing code segments **Q.** - A compiler designer is trying to decide between two code sequence for a particular computer - Which code sequence executes the most instructions? - Which will be faster? - What is the CPI for each sequence? ![](https://hackmd.io/_uploads/rysoEzOk6.png =x65)![](https://hackmd.io/_uploads/S1H24G_yT.png =x65) **A.** - The number of executed instructions - Sequence 1: 2 + 1 + 2 = 5 - Sequence 2: 4 + 1 + 1 = 6 **more !** - Execution time - CPU clock cycles~1~ = (2 $\times$ 1) + (1 $\times$ 2) + (2 $\times$ 3) = 10 cycles - CPU clock cycles~2~ = (4 $\times$ 1) + (1 $\times$ 2) + (1 $\times$ 3) = 9 cycles **faster !** - CPI - CPI~1~ = CPU clock cycles~1~ / Instruction count~1~ = 10/5 = 2 - CPI~2~ = CPU clock cycles~2~ / Instruction count~2~ = 9/6 = 1.5 ::: ## Chap 2 - 定點數的進位轉換 - 浮點數加減乘除運算 - IEEE 754 的運算 - 定點數的乘除法 (表格)