Cosc 5/4755 Algorithms Size and efficiency. Why? On modern desktops and laptops, – Memory size gets larger and larger 8 GBs is now common and 16GBs considered.

Slides:



Advertisements
Similar presentations
Writing Modern C++ Marc Grégoire Software Architect April 3 rd 2012.
Advertisements

Instruction Set Design
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
Recursion CS 367 – Introduction to Data Structures.
Written by: Dr. JJ Shepherd
. Smart Pointers. Memory Management u One of the major issues in writing C/C++ code is managing dynamically allocated memory u Biggest question is how.
Chapter 1 - An Introduction to Computers and Problem Solving
Analysis of Algorithms. Time and space To analyze an algorithm means: –developing a formula for predicting how fast an algorithm is, based on the size.
Computer Science 1620 Programming & Problem Solving.
A Data Locality Optimizing Algorithm based on A Data Locality Optimizing Algorithm by Michael E. Wolf and Monica S. Lam.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Elementary Data Structures and Algorithms
Android Tutorial Android Written in Java Utilizes Dalvik VM – Just in time (JIT) compilation since Android 2.2.
Chapter 8: Introduction to High-Level Language Programming Invitation to Computer Science, C++ Version, Fourth Edition.
Prince Sultan College For Woman
Chapter 1 Algorithm Analysis
Programming Languages and Paradigms Object-Oriented Programming.
5.1 and 5.4 through 5.6 Various Things. Terminology Identifiers: a name representing a variable, class name, method name, etc. Operand: a named memory.
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction to Computer Science.
Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is a heap? What are best-fit, first-fit, worst-fit, and.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
CSC204 – Programming I Lecture 4 August 28, 2002.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management (CTM 2.5) Carlos Varela RPI April 6, 2015.
Cosc 2150: Computer Organization Chapter 11: Performance Measurement.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
WEB DESIGN AND PROGRAMMING Advanced CSS Techniques.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
IT253: Computer Organization
Chapter 25: Code-Tuning Strategies. Chapter 25  Code tuning is one way of improving a program’s performance, You can often find other ways to improve.
CS 3500 L Performance l Code Complete 2 – Chapters 25/26 and Chapter 7 of K&P l Compare today to 44 years ago – The Burroughs B1700 – circa 1974.
Outline Announcements: –HW III due Friday! –HW II returned soon Software performance Architecture & performance Measuring performance.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Garbage Collection and Classloading Java Garbage Collectors  Eden Space  Surviver Space  Tenured Gen  Perm Gen  Garbage Collection Notes Classloading.
Homework due Test the random number generator Create a 1D array of n ints Fill the array with random numbers between 0 and 100 Compute and report the average.
Compiler Optimizations ECE 454 Computer Systems Programming Topics: The Role of the Compiler Common Compiler (Automatic) Code Optimizations Cristiana Amza.
Georgia Institute of Technology Speed part 4 Barb Ericson Georgia Institute of Technology May 2006.
Processor Structure and Function Chapter8:. CPU Structure  CPU must:  Fetch instructions –Read instruction from memory  Interpret instructions –Instruction.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
Introduction to OOP CPS235: Introduction.
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
Written by: Dr. JJ Shepherd
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.
Optimization. How to Optimize Code Conventional Wisdom: 1.Don't do it 2.(For experts only) Don't do it yet.
Introduction to Programming and App Inventor. Introduction What is a computer program? Introducing App Inventor Getting hands on with App Inventor.
An Introduction to Computers and Visual Basic
Machine code Recall that all a computer recognises is binary code.
Introduction to Algorithms
Chapter 5 Conclusion CIS 61.
Introduction to Operating System (OS)
Introduction to Computing
An Introduction to Computers and Visual Basic
Design III Chapter 13 9/20/2018 Crowley OS Chap. 13.
Chapter 8: Main Memory.
CMSC 341 Prof. Michael Neary
Lecture 14 Virtual Memory and the Alpha Memory Hierarchy
Introduction to Computer Systems
CIS 488/588 Bruce R. Maxim UM-Dearborn
Algorithm Discovery and Design
Andy Wang COP 5611 Advanced Operating Systems
Chapter 2: Operating-System Structures
An Introduction to Computers and Visual Basic
Tonga Institute of Higher Education IT 141: Information Systems
Yan Shi CS/SE 2630 Lecture Notes
Tonga Institute of Higher Education IT 141: Information Systems
Chapter 2: Operating-System Structures
Optimization.
Introduction to Optimization
Presentation transcript:

Cosc 5/4755 Algorithms Size and efficiency

Why? On modern desktops and laptops, – Memory size gets larger and larger 8 GBs is now common and 16GBs considered reasonable. – Processor speeds of current systems, are considered to be so fast, that many now don't see the point in algorithm efficiency. About 76,383 MIPS for Intel I7 Only the algorithms with "massive" about of data to process With phones – Average memory: 128MB for most smart phones (for all applications). – ARM processor v7a is about 2,000 MIPS (Droid and palm pre cpus) Reference: Pentium III did about 1,350 MIPS. Note Blackberry, Iphone uses the ARM v6, about 740 MIPS

Meaning? You must be much more careful about your algorithms and memory management together. – You need to really think about the big-O of your algorithms and how much memory it takes. – Think about the number of nested calls you make. Some really fast algorithms are recursive, but you maybe using far more memory for every call. You may need to use a slower algorithm, because it's memory needs are much less. Java helps a lot, because it uses call by reference by default except primitive data types.

memory management tips A lot of object creation and destruction can lead to a fragmented heap, which reduces the ability to create further objects. – Reuse your objects, when ever possible. – Use as few objects as possible. Dispose of Objects; close the database, files, and the network connections as soon as they are no longer needed. Use class destructors to ensure everything's is correctly disposed. – Dereference objects (set them to null) when they're no longer useful so they will be garbage-collected. You can use System.gc() to jump-start or push the garbage collection process. (but not recommended)

memory management tips (2) Use public variables in your classes, rather than using accessors. – This is technically bad programming practice, but it saves bytecode space. Be extra careful to place things in memory only when they are in use. – For example, discard an introduction splash screen after display. Try to reduce the number of classes used. – Combine classes into one if they vary only slightly in behavior. Every class adds size overheads.

Blackberry Devices From blackberry developer's blog Find that memory leak, Part 1 and Part 2 – ry-Developer-s-Blog/How-to-find-that-memory- leak-Part-One/ba-p/ – ry-Developer-s-Blog/How-To-Find-That-Memory- Leak-Part-Two-Detecting-The-Leak/ba-p/432660

performance tips. Whenever possible don’t draw off the screen size – Figure out the width and height (when possible) – so that the system is not using clipping algorithms. Use Double buffering is possible by using an offscreen image the size of the screen. – Most blackberry and android double buffer by default, but you should check.

Performance Tips (2) Minimize redundant arithmetic operations example: Rotating a point around a center double radians = (Math.PI/180); //where px1 and py1 are distance from a center point xc, yc int nx1 = (int) (px1 * Math.cos(radians*angle) - py1 * Math.sin(radians*angle)); int ny1 = (int) (px1 * Math.sin(radians*angle) + py1 * Math.cos(radians*angle)); x1 = xc + nx1; y1 = yc + ny1; better Performance method double radians = Math.toRadians(angle); x1 = xc + (int) (px1 * Math.cos(radians) - py1 * Math.sin(radians)); y1 = yc + (int) (px1 * Math.sin(radians) + py1 * Math.cos(radians)); //If rotating more then one point, then create a variable for Math.cos(radians) & Math.sin(radians) and/or running this code often. //Note also removed an unnecessary variables as well for memory.

Performance Tips (3) Minimize method/function calls wherever possible. – If you repeating call the same method, which returns same the same information then – store that information in a variable for use This, of course, is a trade off with memory use, since you are using more memory for variables.

Performance Tips (4) While you are allowed, DON’T create variables in the loops For (int i=0; … bad Create all the variables at the beginning of the method/function, then reuse them. – Remember minimize you memory use, maximize your speed. Variable creation is slower and uses more memory, then just reusing one.

Code Optimization The best hardware and compilers will never equal the abilities of a human being who has mastered the science of effective algorithm and coding design. Don’t forget your big O measurements. – Operation counting can enhance program performance. – With this method, you count the number of instruction types executed in a loop then determine the number of machine cycles for each instruction.

Code Optimization Loop fusion combines loops that use the same data elements, possibly improving cache performance. For example: becomes for (i = 0; i < N; i++) C[i] = A[i] + B[i]; for (i = 0; i < N; i++) D[i] = E[i] + C[i]; for (i = 0; i < N; i++) { C[i] = A[i] + B[i]; D[i] = E[i] + C[i]; }

Code Optimization Loop fission splits large loops into smaller ones to reduce data dependencies and resource conflicts. A loop fission technique known as loop peeling removes the beginning and ending loop statements. For example: A[1] = 0; for (i = 2; i < N; i++) A[i] = A[i] + 8; A[N] = N; for (i = 1; i < N+1; i++) { if (i==1) A[i] = 0; else if (i == N) A[i] = N; else A[i] = A[i] + 8; } becomes

Code Optimization There are a number of rules of thumb for getting the most out of program performance. Optimization efforts pay the biggest dividends when they are applied to code segments that are executed the most frequently. In short, try to make the common cases fast.

Conflicting info There is any number of conflicting performance and memory info – The big one is to inherit or not to inherit classes. Many say to collapse the inheritance tree as best as you can for both memory use and performance Why others, say inheritance saves memory and improves performance – Answer?

User Interfaces tips Attempt to create applications that can accomplish 80% or more of their operations through the touch of a single key/button or the "tap" or touch of the stylus to the screen. Trying to manipulate a very small scroll bar on a small screen can be an exercise in hand-eye coordination. Horizontal scrolling should be avoided at all costs. Use "jump-to" buttons rather than scrollbars. Try to avoid having the user remember any data, or worse, having to compare data across screens.

References Java ME Performance Tuning – e.shtml Many more web sites – google search Java ME performance issues java ME memory issues or improving memory use

Q A &