Lecture08: Performance 5/9/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C 1 Administrative Items • Assignment #7 is on the course homepage and due 5/16. 2 Optimizing • When speed really matters, C is the language to use – Can be 10-100x faster than Java or Matlab 3 Optimizing • In C language, Compile Source code Execute 00001001 00100100 10001001 00111110 01000011 Executable (binary) Action 1 Action 2 4 Optimizing • In Matlab or Python, No Compilation Execute int a; int b; b=3; a=b+5; 00001001 00100100 Action 1 Action 2 5 Optimizing • When speed really matters, C is the language to use – Can be 10-100x faster than Java or Matlab • But writing something in C doesn't guarantee speed – It's up to you to write efficient code 6 Optimizing • When speed really matters, C is the language to use – Can be 10-100x faster than Java or Matlab • But writing something in C doesn't guarantee speed – It's up to you to write efficient code • There are two general strategies for optimization – Use a better algorithm or different data structures – Write code that implements the algorithm more efficiently 7 Bitwise Operations & 1 0 | 1 0 1 1 0 1 1 1 0 0 0 0 1 0 Or And ^ 1 0 1 1 0 Xor 0 0 1 ~ 1 0 0 1 Not 8 Bitwise Operations • Hexadecimal integer with base of 16. – 0-9 represents values 0-9 – A-F represents values 10-15 a = 0x6A b = 0xC2 // 0110 1010 // 1100 0010 106 194 c=a&b // c = 0x42, bitwise and 0110 1010 & 1100 0010 --------------------0100 0010 9 Bitwise Operations • Hexadecimal integer with base of 16. – 0-9 represents values 0-9 – A-F represents values 10-15 a = 0x6A b = 0xC2 // 0110 1010 // 1100 0010 106 194 c = !a // c = 0, operator not if a is not 0 !a is 0 if a is 0 !a is 1 10 Bitwise Operations • Hexadecimal integer with base of 16. – 0-9 represents values 0-9 – A-F represents values 10-15 a = 0x6A b = 0xC2 // 0110 1010 // 1100 0010 106 194 c =a | b // c = 0xDA, bitwise or 0110 1010 & 1100 0010 --------------------1110 1010 11 Bitwise Operations • Hexadecimal integer with base of 16. – 0-9 represents values 0-9 – A-F represents values 10-15 a = 0x6A b = 0xC2 // 0110 1010 // 1100 0010 106 194 c = ~a // c = 0x95, bitwise not ~ 0110 1010 --------------------1001 0101 12 Bitwise Operations • Hexadecimal integer with base of 16. – 0-9 represents values 0-9 – A-F represents values 10-15 a = 0x6A b = 0xC2 // 0110 1010 // 1100 0010 106 194 c=a^b // c = 0xA8, bitwise xor 0110 1010 ^ 1100 0010 --------------------1010 1000 13 Bitwise Operations • Hexadecimal integer with base of 16. – 0-9 represents values 0-9 – A-F represents values 10-15 a = 0x6A b = 0xC2 // 0110 1010 // 1100 0010 106 194 c = (a << 2) // c = 0xA8, left shift by 2 bits 0110 1010 1010 1000 c = (a >> 3) // c = 0x0D, right shift by 3 bits 0110 1010 0000 1101 14 Don’t Get Confused! x = 0x66, y = 0x93 x & y = 0x02 x | y = 0xF7 ~x | ~y = 0xFD x & !y = 0x00 x && y = 0x01 x || y = 0x01 !x || !y = 0x00 x && ~y = 0x01 15 Bitwise Operations • Bitwise operations are fast. – x * 256 is the same as x << 8 – x % 256 is the same as ? 16 Inefficient Loop void to_lower(char *s) { int i; for (i = 0; i < strlen(s); ++i) { if ('A' <= s[i] && s[i] <= 'Z') { s[i] -= ('A' - 'a'); } } } • How many times strlen(s) will be computed? – strlen() computes in O(n). – The function seems to be O(n) but it is actually O(n2). 17 More efficient loop void to_lower(char *s) { int i; int len = strlen(s); for (i = 0; i < len; ++i) { if ('A' <= s[i] && s[i] <= 'Z') { s[i] -= ('A' - 'a'); } } } • In this function, it computes O(n). 18 Cache-friendly Code Comparing these two codes // cache-friendly int i, j; for (i = 0; i < 10; ++i) { for (j = 0; j < 20; ++j) { a[i][j] += 10; } } // not cache-friendly int i, j; for (j = 0; j < 20; ++j) { for (i = 0; i < 10; ++i) { a[i][j] += 10; } } 19 Cache-friendly Code • An example of 2-dimension A[4][3]. A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2] A[2][0] … 20