Tuesday, 1 September If you are still using the “default” password that was assigned when your account was created, CHANGE IT NOW! (It can be the same as your email password.)
Tuesday, 1 September ● REMINDER: Department coffee/tea this afternoon at 2 p.m. (Alden 102). BE THERE! ● TODAY: Internship lunch ● THUR: Pizza lunch with Acutec recruiters (sign-up sheet will be passed around) ● Questions about Monday’s lab? ● Thursday: “Mock quiz” on K&R 1.6 (“arrays”) ● For Thursday, read K&R 1.6, 1.7 ● Today: A bit more C; performance
A Bit More C Why are we spending time on things like “getchar()” and “putchar()” when there are much more powerful functions for doing input and output? Because when we start looking at how the computer does things at the machine level, we are going to need to think about this kind of primitive “character-at-a-time” processing. For instance, How does the computer convert the characters “3821” (in, say, an assignment statement such as “ i=3821; ”) into the actual numeric value 3821?
A Bit More C It has to convert from character codes (ASCII) to integers. ASCII value Character ASCII value Character 48 67 ‘0’ ‘C’ 49 ... ‘1’ ... 50 90 ‘2’ ‘Z’ ... ... ... ... 57 97 ‘9’ ‘a’ ... 98 ... ‘b’ 65 ... ‘A’ ... 66 122 ‘B’ ‘z’
A Bit More C #include <stdio.h> main() { int c; /* input character--assumed to be a digit */ int digit; /* the decimal digit corresponding to c */ int value = 0; /* value of the input string as an int */ while ((c = getchar()) != '\n') { if (c < '0' || c > '9') { /* Check for error in input */ printf("Error--non-digit in input\n"); break; } digit = c - '0'; /* Convert ASCII to digit */ value = 10 * value + digit; /* Combine with previous digits */ } printf("Value of string: %d\n",value); }
A Bit More C c = '5' (ASCII value 53) digit = 5 value = 5 c = '0' (ASCII value 48) digit = 0 value = 50 c = '8' (ASCII value 56) rroos$./asciitoint digit = 8 value = 508 5082 Value of string: 5082 c = '2' (ASCII value 50) digit = 2 value = 5082
A Bit More C In C, characters are treating like integers. All of the following are perfectly legal (and even make sense): int c, position; c = getchar(); /* read in a character, e.g, ‘A’ */ printf(“char = ‘%c’, ASCII value = %d\n”,c,c); position = c - ‘A’; printf(“position in alphabet: %d\n”,position); OUTPUT (assuming input of ‘A’): char = ‘A’, ASCII value = 65 position in alphabet: 0
Performance (P&H, 1.6--1.7) For the next few slides we borrow from the publisher’s resources! (I have edited these slightly from the originals.)
Defining Performance Performance §1.6 ■ Which airplane has the best performance?
Response Time and Throughput ■ Response time ■ How long it takes to do a task ■ Throughput ■ Total work done per unit time ■ e.g., tasks/transactions/… per hour ■ How are response time and throughput affected by ■ Replacing the processor with a faster version? ■ Adding more processors? ■ We’ll focus on response time for now…
Relative Performance ■ Define Performance = 1/Execution Time ■ “X is n time faster than Y” ■ Example: time taken to run a program ■ 10s on A, 15s on B ■ Execution Time B / Execution Time A = 15s / 10s = 1.5 ■ So A is 1.5 times faster than B
Measuring Execution Time ■ Elapsed time ■ Total response time, including all aspects ■ Processing, I/O, OS overhead, idle time ■ Determines system performance ■ CPU time ■ Time spent processing a given job ■ Discounts I/O time, other jobs’ shares ■ Comprises user CPU time and system CPU time ■ Different programs are affected differently by CPU and system performance
CPU Clocking ■ Operation of digital hardware governed by a constant-rate clock Clock period Clock (cycles) Data transfer and computation Update state Hz = ■ Clock period: duration of a clock cycle “Hertz” = “cycles per ■ e.g., 250ps = 0.25ns = 250×10 –12 s second” ■ Clock frequency (rate): cycles per second ■ e.g., 4.0GHz = 4000MHz = 4.0×10 9 Hz
CPU Time ■ Performance improved by ■ Reducing number of clock cycles ■ Increasing clock rate ■ Hardware designer must often trade off clock rate against cycle count
CPU Time Example Computer A: 2GHz clock, 10s CPU time ■ Designing Computer B ■ Aim for 6s CPU time ■ Can do faster clock, but causes 1.2 × clock cycles ■ How fast must Computer B clock be? ■
Instruction Count and CPI ISA = “Instruction Set Architecture” ■ Instruction Count for a program ■ Determined by program, ISA and compiler ■ Average cycles per instruction CPI = “Cycles Per ■ Determined by CPU hardware Instruction” ■ If different instructions have different CPI ■ Average CPI affected by instruction mix
CPI Example ■ Computer A: Cycle Time = 250ps, CPI = 2.0 ■ Computer B: Cycle Time = 500ps, CPI = 1.2 ■ Same ISA ■ Which is faster, and by how much? A is faster… …by this much
CPI in More Detail ■ If different instruction classes take different numbers of cycles ■ Weighted average CPI Relative frequency
CPI Example ■ Alternative compiled code sequences using instructions in classes A, B, C Class A B C CPI for class 1 2 3 IC in sequence 1 2 1 2 IC in sequence 2 4 1 1 ■ Sequence 1: IC = 5 ■ Sequence 2: IC = 6 ■ Clock Cycles ■ Clock Cycles = 2×1 + 1×2 + 2×3 = 4×1 + 1×2 + 1×3 = 10 = 9 ■ Avg. CPI = 10/5 = 2.0 ■ Avg. CPI = 9/6 = 1.5
Performance Summary The BIG Picture ■ Performance depends on ■ Algorithm: affects IC, possibly CPI ■ Programming language: affects IC, CPI Cycle time? ■ Compiler: affects IC, CPI ■ Instruction set architecture: affects IC, CPI, T c
In-Class Exercise Exercise 1.5, page 55:
Supplement to 1 September slides Solution to in-class exercise: PI: P2: P3:
Supplement to 1 September slides Solution to in-class exercise: PI: P2: P3:
Supplement to 1 September slides Solution to in-class exercise: We want: (new execution time) = .7(old execution time) Let’s consider time to execute a single instruction. If C = cycles per instruction in old machine, then 1.2xC = cycles per instruction in new machine. Let R = clock rate in old machine, let x = clock rate in new machine. Then:
Supplement to 1 September slides Solution to in-class exercise: P1: P2: P3:
Recommend
More recommend