Download presentation
Presentation is loading. Please wait.
Published byClarence Allen Modified over 9 years ago
1
Code-Tuning By Jacob Shattuck
2
Code size/complexity vs computation resource utilization A classic example: Bubblesort A classic example: Bubblesort const int ARRAY_LENGTH = 12; // bubblesort int temp; for ( int i = 0 ; i < ARRAY_LENGTH ; i ++ ) { if ( ARRAY1[i] > ARRAY1[i+1] ) { // needs to be swapped // swap temp = ARRAY1[i]; ARRAY1[i] = ARRAY1[i+1]; ARRAY[i+1] = temp; // FLAG a swap has been made (i = 0)--; }
3
The less known: For loop array assignment. The less known: For loop array assignment. //the easy way for (int i = 0 ; i < ARRAY_SIZE ; i ++ ) myarr[i] = i; //the faster way myarr[0] = 0; myarr[1] = 1;... myarr[12] = 12; VB 63% faster Java 74% faster
4
When to make such optimizations High level language & time High level language & time Low level language & complexity Low level language & complexity Also: Good programming practice (low complexity) Also: Good programming practice (low complexity)
5
Avoid bad practice Avoid disk caching in program and out Avoid disk caching in program and out Move row by column (and keep in mind for general practice) Move row by column (and keep in mind for general practice)
6
Interpreted Ruby, XML, query, and all web languages. Ruby, XML, query, and all web languages.
7
Abstraction & Calls The more abstraction, the longer processing time. But again, remember good programming practice. The more abstraction, the longer processing time. But again, remember good programming practice. Know that division and special calls (like sqrt, sin, etc) take far longer than + - and *. Know that division and special calls (like sqrt, sin, etc) take far longer than + - and *.
8
Code-Tuning Checklist Would changing the program’s requirements help the program run faster? (ie: ask for array size instead of trying to find a marker at the array’s end). Would changing the program’s requirements help the program run faster? (ie: ask for array size instead of trying to find a marker at the array’s end). Have you considered improving performance by modifying the program’s design? Have you considered improving performance by modifying the program’s design? Have you considered improving performance by modifying the class design? (multiple inheritance will take longer.) Have you considered improving performance by modifying the class design? (multiple inheritance will take longer.)
9
Know your language! Ex: Short circuit testing Ex: Short circuit testing if ( 5 < x ) and ( x < 10 ) then... is turned into if ( 5 < x ) then if ( 5 < 10 ) then... Built into C++ with standard checking, Java can also have short circuit testing.
10
Short circuiting continued Problems? negativeInputFound = false; for ( int i = 0 ; i < count ; i ++ ) { if ( input[ i ] < 0 ) { negativeInputFound = true; }
11
Short circuiting – the last one Solutions! Add a break statement Add a break statement If your language doesn’t have a break statement, emulate a break statement with a goto. If your language doesn’t have a break statement, emulate a break statement with a goto. Change the for loop to a while loop Change the for loop to a while loop negativeInputFound = false; for ( int i = 0 ; i < count ; i ++ ) { if ( input[ i ] < 0 ) { negativeInputFound = true; }
12
Checklist #2 Substitute complicated logic for table lookups (add heuristics). Substitute complicated logic for table lookups (add heuristics). Jam (combine) loops (when you can) Jam (combine) loops (when you can) Use integer instead of floating point Use integer instead of floating point Initialize data at compile time Initialize data at compile time Use constants of the correct type Use constants of the correct type Precompute results (heuristics) Precompute results (heuristics) Perform common subexpressions once if possible Perform common subexpressions once if possible Translate key routines into low-level language Translate key routines into low-level language
13
The final step As mentioned before… Assembly is faster than C++, C#, or Java. As mentioned before… Assembly is faster than C++, C#, or Java.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.