introduction to c
play

Introduction to C Performance Instructor: Yin Lou 02/07/2011 - PowerPoint PPT Presentation

Introduction to C Performance Instructor: Yin Lou 02/07/2011 Introduction to C CS 2022, Spring 2011, Lecture 7 Optimizing When speed really matters, C is the language to use Can be 10-100x faster than Java or Matlab Introduction to C


  1. Introduction to C Performance Instructor: Yin Lou 02/07/2011 Introduction to C CS 2022, Spring 2011, Lecture 7

  2. Optimizing ◮ When speed really matters, C is the language to use ◮ Can be 10-100x faster than Java or Matlab Introduction to C CS 2022, Spring 2011, Lecture 7

  3. 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 Introduction to C CS 2022, Spring 2011, Lecture 7

  4. 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 Introduction to C CS 2022, Spring 2011, Lecture 7

  5. Bitwise Operations a = 0x00FF b = 0xF000 a & b = 0x0000 // bitwise and a | b = 0xF0FF // bitwise or ~a = 0xFF00 // bitwise not a ^ b = 0xF0FF // bitwise xor a << 4 = 0x0FF0 // left shift by 4 bits a >> 4 = 0x000F // right shift by 4 bits Introduction to C CS 2022, Spring 2011, Lecture 7

  6. Right Shift ◮ Arithmetic right shift: Copy the left most bit ◮ Logical right shift: Place 0s in the left Introduction to C CS 2022, Spring 2011, Lecture 7

  7. Right Shift ◮ Arithmetic right shift: Copy the left most bit ◮ Logical right shift: Place 0s in the left Example int a = 0xF000; a >> 4 = 0xFF00 // Arithmetic right shift a >> 4 = 0x0F00 // Logical right shift Introduction to C CS 2022, Spring 2011, Lecture 7

  8. Right Shift ◮ Arithmetic right shift: Copy the left most bit ◮ Logical right shift: Place 0s in the left Example int a = 0xF000; a >> 4 = 0xFF00 // Arithmetic right shift a >> 4 = 0x0F00 // Logical right shift Unfortunately, for signed data, there’s no standard which one to use, but for almost all machines, arithmetic right shift is used. Introduction to C CS 2022, Spring 2011, Lecture 7

  9. Don’t Get Confused! x = 0x66, y = 0x93 x & y = 0x02 x && y = 0x01 x | y = 0xF7 x || y = 0x01 ~x | ~y = 0xFD !x || !y = 0x00 x & !y = 0x00 x && ~y = 0x01 Introduction to C CS 2022, Spring 2011, Lecture 7

  10. Bitwise Operations ◮ Bitwise operations are fast. Introduction to C CS 2022, Spring 2011, Lecture 7

  11. Bitwise Operations ◮ Bitwise operations are fast. ◮ x * 256 ⇔ x << 8 ◮ x % 256 ⇔ ? Introduction to C CS 2022, Spring 2011, Lecture 7

  12. Struct and Union struct node { struct node *left; struct node *right; double data; // only leaf node has data }; Each node needs 16 bytes, but almost half of them are wasted. Introduction to C CS 2022, Spring 2011, Lecture 7

  13. Struct and Union struct node { struct node *left; struct node *right; double data; // only leaf node has data }; Each node needs 16 bytes, but almost half of them are wasted. union node { struct { union node *left; union node *right; } internal; double data; }; Introduction to C CS 2022, Spring 2011, Lecture 7

  14. Struct and Union But now we have no way to tell which node is leaf. struct node { int is_leaf; union { struct { union node *left; union node *right; } internal; double data; } info; }; Introduction to C CS 2022, Spring 2011, Lecture 7

  15. Struct and Union But now we have no way to tell which node is leaf. struct node { int is_leaf; union { struct { union node *left; union node *right; } internal; double data; } info; }; Now each node only needs 12 bytes. Introduction to C CS 2022, Spring 2011, Lecture 7

  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’); } } } Introduction to C CS 2022, Spring 2011, Lecture 7

  17. 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’); } } } strlen(s) will be computed for each run of the loop. The function seems to be O ( n ) but it is actually O ( n 2 ). Introduction to C CS 2022, Spring 2011, Lecture 7

  18. Code Motion 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’); } } } Introduction to C CS 2022, Spring 2011, Lecture 7

  19. Cache ◮ A subset of data in main memory ◮ Faster access than main memory ◮ You can view main memory as a cache to the hard disk Introduction to C CS 2022, Spring 2011, Lecture 7

  20. 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; } } Introduction to C CS 2022, Spring 2011, Lecture 7

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend