A little bit about optimization, 2d array, 1d array used as 2d, register volatile union .

Slides:



Advertisements
Similar presentations
C Programming Course Overview
Advertisements

Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
C++ Basics Variables, Identifiers, Assignments, Input/Output.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
27-Jun-15 Profiling code, Timing Methods. Optimization Optimization is the process of making a program as fast (or as small) as possible Here’s what the.
30-Jun-15 Profiling. Optimization Optimization is the process of making a program as fast (or as small) as possible Here’s what the experts say about.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Modules and Scope Gabriel Hugh Elkaim Spring 2013.
Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
CS 2130 Lecture 5 Storage Classes Scope. C Programming C is not just another programming language C was designed for systems programming like writing.
1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through.
Blackfin Array Handling Part 1 Making an array of Zeros void MakeZeroASM(int foo[ ], int N);
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Survey of Program Compilation and Execution Bangor High School Ali Shareef 2/28/06.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
Compiling “premature optimization is the root of all evil.” -Donald Knuth.
CSE 351 Caches. Before we start… A lot of people confused lea and mov on the midterm Totally understandable, but it’s important to make the distinction.
Enum. enum – a new type 2 enum is a set of constant int values, that defines a type: enum Season { WINTER,// = 0 by default SPRING,// = WINTER + 1 SUMMER,//
. A little bit about optimization, 2d array, 1d array used as 2d, register and volatile.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
C++ Lesson 1.
Asst.Prof.Dr. Tayfun ÖZGÜR
A little bit about optimization, 2d array, 1d array used as 2d, register volatile union .
Arrays Low level collections.
Data types Data types Basic types
Introduction To Computer Systems
C Programming Course Overview
LESSON 3 IO, Variables and Operators
Operator overloading Conversions friend inline
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Pointers in C.
Classes & Objects.
Chapter 5 Conclusion CIS 61.
Workshop in Nihzny Novgorod State University Activity Report
Student Book An Introduction
Introduction to C Programming Language
Engineering Innovation Center
CMSC 104, Section 4 Richard Chang
Baremetal C Programming for Embedded Systems
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
Advanced Programming Basics
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
6.12 Default Arguments.
Lecture 18 Arrays and Pointer Arithmetic
Variables in C Topics Naming Variables Declaring Variables
Functions 2: Stupid Function Tricks
Introduction to Programming
Explaining issues with DCremoval( )
Practical Session 8, Memory Management 2
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Dynamic Memory A whole heap of fun….
Programming Language C Language.
C Miscellaneous Programs Prabhat Kumar Padhy
Suggested self-checks: Section 7.11 #1-11
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
C Data Types and Variable
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
C Language B. DHIVYA 17PCA140 II MCA.
CSCE 206 Lab Structured Programming in C
ENERGY 211 / CME 211 Lecture 11 October 15, 2008.
Practical Session 9, Memory Management continues
Introduction to Pointers
Extra C Material Based on material in: The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. 
Presentation transcript:

A little bit about optimization, 2d array, 1d array used as 2d, register volatile union .

What smart people say about Premature optimization… Premature optimization is the root of all evil (or at least most of it) in programming – Donald Knuth

So, what to do? Check if you need to optimize Remember to “turn off” debugging (#define NDEBUG) Check what your compiler can do for you on your specific hardware (-O3 -march=pentium4 -mfpmath=sse, inlining of functions, ...) Profile: check where to optimize Use common techniques such as cache,...

1d Array as 2d array int* getElementAddr(int* arr, int r, int c) { return (arr+ r*COLS_NUM + c); } void foo(int* arr) { ... *(getElementAddr(arr,r,c)) 1,2 1,1 1,0 0,2 0,1 0,0

1d Array as 2d array int* getElementAddr(int* arr, int r, int c) { return (arr+ r*COLS_NUM + c); } void foo(int* arr) { ... *(getElementAddr(arr,r,c)) What can we do if we use a specific r and c many times and this is the bottleneck section?

1d Array as 2d array Use cache ! void foo(int* arr) { ... size_t cache_index= r*COLS_NUM + c; ... ... *(arr + cache_index) } Use cache !

register Register - a small amount of very fast memory. Provides quick access to commonly used values. The register keyword specifies that the variable is to be stored in a machine register, if possible. Experience has shown that the compiler usually knows much better than humans what should go into registers and when. There is actually an hierarchy of caches (L1, L2,…). We are not going to learn this in this course.

Volatile variables that may be modified externally from the declaring object. Variables declared to be volatile will not be optimized by the compiler because their value can change at any time.

Volatile example void foo(void) { int* addr; addr= INPUT_ADDRESS; while (*addr != 255) ; } Before optimization

Volatile example void foo(void) { int* addr; addr= INPUT_ADDRESS; while (1) ; } After optimization

Volatile example void foo(void) { volatile int* addr; addr= INPUT_ADDRESS; *addr = 0; while (*addr != 255) ; } After optimization using volatile

union

union – a new type typedef union MyUnion { int i_val; double d_val; A type that keeps (in different times) different types typedef union MyUnion { int i_val; double d_val; } MyUnion;

union – a new type, usage MyUnion u; u.i_val= 3; typedef union MyUnion { int i_val; double d_val; } MyUnion; MyUnion u; u.i_val= 3; printf("%d\n", u.i_val); u.d_val= 3.22; printf("%f\n", u.d_val); ... ...