Optimised C/C++. Overview of DS General code Functions Mathematics.

Slides:



Advertisements
Similar presentations
2.1 Program Construction In Java
Advertisements

Introduction to C Programming
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
CS0004: Introduction to Programming Repetition – Do Loops.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
DCO1 Performance Measurement and Improvement Lecture 7.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Loops – While, Do, For Repetition Statements Introduction to Arrays
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Lecture 4. RAM Model, Space and Time Complexity
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming  To allocate scarce memory resources.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
I Power Higher Computing Software Development High Level Language Constructs.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Optimization of C Code The C for Speed
Controlling Computers with Programs When you create a computer program you are creating a set of instructions that tell the computer exactly and completely.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Review : C Programming Language
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Information and Computer Sciences University of Hawaii, Manoa
Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Chapter 7: User-Defined Functions II
Operating Systems (CS 340 D)
Object Oriented Programming
Chapter 12 Variables and Operators
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Computing with C# and the .NET Framework
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Operating Systems (CS 340 D)
Arithmetic operations, decisions and looping
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Recursion Data Structures.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Dr. Mustafa Cem Kasapbaşı
Memory management Explain how memory is managed in a typical modern computer system (virtual memory, paging and segmentation should be described.
COP 3330 Object-oriented Programming in C++
CSC215 Lecture Control Flow.
CSE 206 Course Review.
Procedure Linkages Standard procedure linkage Procedure has
Corresponds with Chapter 5
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

Optimised C/C++

Overview of DS General code Functions Mathematics

General CPU based copies throw everything out of the cache, meaning that cache misses will occur after a transfer.

General Before any code level optimisations consider: Does it need to be optimised Is the algorithm sufficient Write simple/clear code

General Code Use native variable sizes e.g. DS is a 32bit machine, so use ints for calculations Smaller variable sizes (e.g. char, short) have to be converted to 32bit variables before any calculations are carried out. For example: char count; for(count=0;count<128;count++) versus int count; for(count=0;count<128;count++)

General Code Use native variable sizes e.g. DS is a 32bit machine, so use ints for calculations Smaller variable sizes (e.g. char, short) have to be converted to 32bit variables before any calculations are carried out. For example: char a,b; char ans = a + b; versus int a,b; int ans = a + b;

General Code Exploit locality of reference When reading data, the CPU often reads chunks of surrounding data into fast cache (e.g. 32kb) If the next accessed data is not within fast cache (a miss), the CPU must look for it in memory, and load that into cache. If this is done repeatedly (i.e. lots of cache misses), thrashing occurs. This situation can be avoided by structuring your code appropriately. E.g. for (i=0;i<N;i++) { for (j=0;j<N;j++) { ans[j][i] = a[j][i]+b[j][i]; } } for (i=0;i<N;i++) { for (j=0;j<N;j++) { ans[i][j] = a[i][j]+b[i][j]; } }

General Code Global variables: Global variables cannot be cached. Therefore, need to be loaded and stored in a register on each use. If using global variables in a tight game loop, consider storing their value in a local variable before use E.g. int global = 100; //all other code here int local = global; for ( … ) //time critical loop here

General Code Aliases: Consider the code below: void func1( int *data ) { int i; for(i=0; i<10; i++) { anyfunc( *data, i); } Even though data is not modified, compiler does not know this, so has to read data value from memory every access This code can be improved as follows: void func1( int *data ) { int val = *data; int i; for(i=0; i<10; i++) { anyfunc( val, i); }

General Code General calculations: Division – replace with reciprical multiplication (e.g ans = a/b vs. ans = a * 1/b) Power of 2 calculations  If any division or multiplication calculations are a power of 2, they can be replace with a left or right shift. E.g. ans = result * 8 => ans = result ans = result>>3  If using modulo by n which is a power of 2, can replace with a logical & by n-1 e.g. ans = result %8 => ans = result & 7  Even if a number is not a power of 2, it can be subdivided into sub calculations that are E.g. ans = result * 136 => ans = result<<7 + (result<<3) ans = (result*128) + (result*8)

Loops Loop unrolling Reducing the number of iterations a loop takes by increasing the number of instructions in the loop body E.g. for (i = 0 ;i<128;i++) ans = ans * 8; becomes for (i = 0 ;i<32;i++) { ans = ans * 8; ans = ans * 8; ans = ans * 8; ans = ans * 8; }

Loops Loop jamming Combining adjacent loops which iterate over the same range of values E.g. for (i = 0 ;i<128;i++) ans = ans * 8; for (i = 0 ;i<128;i++) otherVal += someArray[i]; becomes for (i = 0 ;i<128;i++) { ans = ans * 8; otherVal += someArray[i]; }

Loops Loop inversion Rewriting loops to run from n to 0, rather than 0 to n. E.g. for (i = 0 ;i<128;i++) becomes i = 128 while(i--) { }

Loops Simplifying loop conditions Execution of a loop can be faster if the loop control conditions are simplified. For example: for(i=0;i<=100;i++) versus for(i=100;i--)

Loops Function looping Used when functions are called inside loops. Rather than calling function inside loop, call function once, and rewrite loop inside function. Removes overhead of function call every time. E.g. for (i=0;i<1000;i++) doFunc(); becomes doFunc() void doFunc() { for (i=0;i<1000;i++) //do something }

Conditional Statements Exploit lazy evaluation: In if statements like (a<c && b<d), make sure the first case is most likely to give false, as the rest will then not be evaluated. In large if statements, make sure the first case is most likely to give true. Prefer switches to if statements: E.g if( val == 1) dostuff1(); else if (val == 2) dostuff2(); else if (val == 3) dostuff3(); switch( val ) { case 1: dostuff1(); break; case 2: dostuff2(); break; case 3: dostuff3(); break; }

Conditional Statements Binary Breakdown: Structure if/else statements to consider blocks of statements, rather than just one statement at a time. For example: if(a==1) { } else if(a==2) { } else if(a==3) { } else if(a==4) { } else if(a==5) { } else if(a==6) { } else if(a==7) { } else if(a==8) { } if(a<=4) { if(a==1) { } else if(a==2) { } else if(a==3) { } else if(a==4) { } } else { if(a==5) { } else if(a==6) { } else if(a==7) { } else if(a==8) { } }

Function Design Keep functions small and simple. Enables the compiler to perform optimisations easily. Make the number of parameters being passed relative to the work done by the function When parameters are passed to a function, they are first stored in fast registers, and then stored on the stack If the work done by a function is small or called often, then the number of parameters passed to it should be minimised. Promote the use of “ leaf ” functions A leaf function is one which does not call any other functions. These can be optimised by a compiler, as it does not have to perform any stack management when calling other functions.

C++ Optimisation Minimise use of virtual functions Minimise constructor overhead: Constructors are called everywhere in C++, even when we don ’ t expect it. Therefore, optimising constructor overhead can go a long way to speeding up code execution. Mechanisms of doing this include:  Pass object parameters by reference (same goes for structs in C)  Prefer prefix (++x) to postfix (x++) operators.  Prefer two-phase to one-phase object creations  Use constructor initialisation lists  Define local variables in the inner most scope  Prefer initialisation over assignment

C++ Optimisation Prefer two-phase to one-phase object creations E.g. do minimal amount of work in actual constructor, and use “ create ” methods to allocate memory etc. Use constructor initialisation lists When constructing objects with other objects, initialisation within the constructor results in lower performance than using an initialisation list.  E.g. myObj (string name,string job) : m_name(name), m_job(job) { //rest of ctor} versus myObj (string name,string job) { m_name = name; m_job = job; }

C++ Optimisation Define local objects in the inner most scope e.g int func(int param) { Obj A; if (param == 1) { A.value += 10; //rest of if statement } //rest of function } int func(int param) { if (param == 1) { Obj A; A.value += 10; } //rest of function }

C++ Optimisation Prefer initialisation over assigment e.g Obj B; //work done on B Obj A; A = B;//requires 2 constructor calls vs. Obj B; //work done on B Obj A = B; //requires 1 constructor call