170 likes | 277 Vues
Cosc 5/4730. 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.
E N D
Cosc5/4730 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: 256MB 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.
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 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 (inti=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: • 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 • A[1] = 0; • for (i = 2; i < N; i++) • A[i] = A[i] + 8; • A[N] = N;
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 • http://www.javaperformancetuning.com/tips/j2me.shtml • Many more web sites • google search • Java ME performance issues • java ME memory issues or improving memory use
Q A &