DCO1 Performance Measurement and Improvement Lecture 7.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 10 SHARED MEMORY.
Dynamic Memory Management
Programming Languages and Paradigms The C Programming Language.
C++ Basics Variables, Identifiers, Assignments, Input/Output.
C Programming - Lecture 5
Malloc Recitation Section K (Kevin Su) November 5 th, 2012.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
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.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
CSC 107 – Programming For Science. The Week’s Goal.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Optimised C/C++. Overview of DS General code Functions Mathematics.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Computer Graphics 3 Lecture 1: Introduction to C/C++ Programming Benjamin Mora 1 University of Wales Swansea Pr. Min Chen Dr. Benjamin Mora.
Optimization of C Code The C for Speed
Maths & Technologies for Games Optimisation for Games 1 CO3303 Week 4.
C LANGUAGE Characteristics of C · Small size
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
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.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
C Short Overview Lembit Jürimägi.
Optimizing Malloc and Free
Building Java Programs Chapter 2
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to Java, and DrJava part 1
Building Java Programs
C Programming Getting started Variables Basic C operators Conditionals
Chapter 2 Programming Basics.
Malloc Lab CSCI 380: Operating Systems
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Module 2 Variables, Data Types and Arithmetic
C Programming - Lecture 5
Programming Languages and Paradigms
C Language B. DHIVYA 17PCA140 II MCA.
Variables and Constants
Presentation transcript:

DCO1 Performance Measurement and Improvement Lecture 7

2 Practical Hints Give you the practical methods to enhance the programming performance Hidden Trouble Fast Allocation and Free

3 Hidden Trouble First look at the memory allocation, malloc() malloc in printf malloc Strings Malloc, memory allocation

4 malloc in printf printf causes malloc to be called in the usual implementation. This can add an unexpected cost. String manipulation is expensive in general whether it is formatting text as in printf, reading ASCII text and converting to numbers, or performing string comparisons Better not to use printf() Use puts()

5 Malloc malloc is (or new in C++) expensive. A common solution is to use static or local variables to avoid allocating memory on the heap. Another solution is to keep a list of objects that need to be allocated often. Then, allocation is just a matter of removing an object from the list, and freeing simply inserts the object on the list Better not to use malloc()

6 Strings Microsoft Foundation Class (MFC) CString class allocates dynamic memory. This is great if you want to avoid managing memory yourself and you want to avoid nasty bugs due to writing data beyond the end of allocated string memory. On the other hand, if you find that memory allocation is taking significant time in an inner loop, you might want to consider allocating a fixed-length character array as local or static data.

7 Fast Allocation and Free to obtain faster performance is to use a large block of memory from which smaller chunks are allocated to compute some result. (heap) After the result is obtained, the entire block is freed. This is fast because: Memory is allocated simply by incrementing the "free" pointer by the number of bytes you need to allocate. There is no need to free each allocated object; you free all objects at once by freeing the entire pool at once. reason s

8 Coding for Speed mainly from this web site Array Indices Aliases Registers Integers Loop Jamming Dynamic Loop Unrolling Faster for() loops Switch Pointers Early loop breaking Misc Using array indices There are many ways to speed up the operation.

9 Array Indices switch ( queue ) { case 0 : letter = 'W'; break; case 1 : letter = 'S'; break; case 2 : letter = 'U'; break; } or may be if ( queue == 0 ) letter = 'W'; else if ( queue == 1 ) letter = 'S'; else letter = 'U'; An example using switch and if-else

10 Array Indices A quicker method is to simply use the value as an index into a character array, eg. static char *classes="WSU"; letter = classes[queue]; In this case, class[0] means W, class[1] means S and class[2] means U

11 Aliases (1) void func1( int *data ) { int i; for(i=0; i<10; i++) { somefunc2( *data, i); } } Not very good

12 Aliases – better change to this void func1( int *data ) { int i; int localdata; localdata = *data; for(i=0; i<10; i++) { somefunc2( localdata, i); } } Better way

13 Registers – computer is good at register allocation Use the "register" declaration whenever you can, eg. register float val; register double dval; register int ival; This will be fster

14 Integers Use unsigned ints instead of ints if you know the value will never be negative. Unsigned int a; is better then int a; Some processors can handle unsigned integer arithmetic considerably faster than signed eg. unsigned int i; instead of int i Integer arithmetic is faster than floating-point operation

15 Loop Jamming Never use two loops where one is enough: for(i=0; i<100; i++) { stuff(); } for(i=0; i<100; i++) { morestuff(); } Better combine them

16 Loop Jamming It would be better to do: for(i=0; i<100; i++) { stuff(); morestuff(); }

17 Example – three loops (0.36ms)

18 Example – one loop (0.31ms)

19 Loop Unrolling and Dynamic Loop Unrolling for(i=0; i<3; i++) { something(i); } is less efficient than something(0); something(1); something(2); It is because the code has to check and increment the value of i.

20 Example – two for loops (0.96ms)

21 Example – one for loop (0.52ms)

22 Faster for loop Ordinarily, you would code a simple for() loop like this: for( i=0; i<10; i++){... } i loops through the values 0,1,2,3,4,5,6,7,8,9 If you don't care about the order of the loop counter, you can do this instead: for( i=10; i--; ) {... } 10, 9, 8, 7, …….. Decrement is faster

23 Faster for loop The syntax is a little strange, but is perfectly legal. The same effect could also be gained by coding: for(i=10; i; i--){ …… } or (to expand it further) for(i=10; i!=0; i--){ …… }

24 Example – int and increment (1.51ms)

25 Example– unsigned int, decrement (1.29ms)

26 Use witch() instead of if...else... For large decisions involving if...else...else..., like this: if( val == 1) dostuff1(); else if (val == 2) dostuff2(); else if (val == 3) dostuff3(); it may be faster to use a switch: switch( val ) { case 1: dostuff1(); break; case 2: dostuff2(); break; case 3: dostuff3(); break; } Better change to case

27 Pointers Whenever possible, pass structures by reference ( ie. pass a pointer to the structure ) void print_data( const bigstruct *data_pointer) {...printf contents of structure... }

28 Early loop breaking This loop searches a list of numbers to see if there is a -99 in it. found = FALSE; for(i=0;i<10000;i++) { if( list[i] == -99 ) { found = TRUE; } } if( found ) printf("Yes, there is a -99. Hooray! \n"); This works well but searches the whole list.

29 Early loop breaking A better way is to abort the search when it is found. found = FALSE; for(i=0; i<10000; i++) { if( list[i] == -99 ) { found = TRUE; break; } } if( found ) printf("Yes, there is a -99. Hooray!\n");

30 Suggestion (1) Avoid using ++ and -- etc. within loop expressions, eg. while(n--){}, as this can sometimes be harder to optimise. Minimize the use of global variables. Declare anything within a file (external to functions) as static, unless it is intended to be global. Use word-size variables if you can, as the machine can work with these better ( instead of char, short, double, bitfields etc. ).

31 Suggestion (2) Don't use recursion. Recursion can be very elegant and neat, but creates many more function calls which can become a large overhead. Avoid the sqrt() square root function in loops - calculating square roots is very CPU intensive. Single dimension arrays are faster than multi- dimensioned arrays. (a[16] is better than a[4][4]) Compilers can often optimise a whole file - avoid splitting off closely related functions into separate files, the compiler will do better if can see both of them together (it might be able to inline the code, for example).

32 Example - without recursion

33 Example - with recursion (366 ms), I already reduced the number of recursions

34 Suggestion (3) Single precision maths may be faster than double precision - there is often a compiler switch for this. (float is better than double unless you really want it.) Floating point multiplication is often faster than division - use val * 0.5 instead of val / 2.0. Addition is quicker than multiplication - use val + val + val instead of val * 3 puts() is quicker than printf(), although less flexible.

35 Example - float (4 bytes) and double (8 bytes)

36 Suggestion (4) Use #defined macros instead of commonly used tiny functions - sometimes the bulk of CPU usage can be tracked down to a small external function being called thousands of times in a tight loop. Replacing it with a macro to perform the same job will remove the overhead of all those function calls, and allow the compiler to be more aggressive in it's optimisation.. Binary/unformatted file access is faster than formatted access, as the machine does not have to convert between human-readable ASCII and machine-readable binary. If you don't actually need to read the data in a file yourself, consider making it a binary file.

37 Summary It is better to write a simple but fast program. There are many ways to speed up the operation in programming.