13
Code-Tuning Code-Tuning By Jacob Shattuck By Jacob Shattuck

Code-Tuning By Jacob Shattuck. Code size/complexity vs computation resource utilization A classic example: Bubblesort A classic example: Bubblesort const

Embed Size (px)

Citation preview

Page 1: Code-Tuning By Jacob Shattuck. Code size/complexity vs computation resource utilization A classic example: Bubblesort A classic example: Bubblesort const

Code-TuningCode-Tuning

By Jacob ShattuckBy Jacob Shattuck

Page 2: Code-Tuning By Jacob Shattuck. Code size/complexity vs computation resource utilization A classic example: Bubblesort A classic example: Bubblesort const

Code size/complexity vs Code size/complexity vs computation resource computation resource

utilizationutilization A classic example: BubblesortA classic example: Bubblesort

const int ARRAY_LENGTH = 12;

// bubblesortint temp;for ( int i = 0 ; i < ARRAY_LENGTH ; i ++ ) {

if ( ARRAY1[i] > ARRAY1[i+1] ) { // needs to be swapped// swaptemp = ARRAY1[i];ARRAY1[i] = ARRAY1[i+1];ARRAY[i+1] = temp;// FLAG a swap has been made(i = 0)--;

}}

Page 3: Code-Tuning By Jacob Shattuck. Code size/complexity vs computation resource utilization A classic example: Bubblesort A classic example: Bubblesort const

The less known: For loop array The less known: For loop array assignment.assignment.//the easy way

for (int i = 0 ; i < ARRAY_SIZE ; i ++ )myarr[i] = i;

//the faster waymyarr[0] = 0;myarr[1] = 1;...myarr[12] = 12;

VB 63% faster

Java 74% faster

Page 4: Code-Tuning By Jacob Shattuck. Code size/complexity vs computation resource utilization A classic example: Bubblesort A classic example: Bubblesort const

When to make such When to make such optimizationsoptimizations

High level language & timeHigh level language & time Low level language & complexityLow level language & complexity

Also: Good programming practice Also: Good programming practice (low complexity)(low complexity)

Page 5: Code-Tuning By Jacob Shattuck. Code size/complexity vs computation resource utilization A classic example: Bubblesort A classic example: Bubblesort const

Avoid bad practiceAvoid bad practice

Avoid disk caching in program and Avoid disk caching in program and outout

Move row by column (and keep in Move row by column (and keep in mind for general practice)mind for general practice)

Page 6: Code-Tuning By Jacob Shattuck. Code size/complexity vs computation resource utilization A classic example: Bubblesort A classic example: Bubblesort const

InterpretedInterpreted

Ruby, XML, query, and all web Ruby, XML, query, and all web languages.languages.

Page 7: Code-Tuning By Jacob Shattuck. Code size/complexity vs computation resource utilization A classic example: Bubblesort A classic example: Bubblesort const

Abstraction & CallsAbstraction & Calls

The more abstraction, the longer The more abstraction, the longer processing time. But again, processing time. But again, remember good programming remember good programming practice.practice.

Know that division and special calls Know that division and special calls (like sqrt, sin, etc) take far longer (like sqrt, sin, etc) take far longer than + - and *.than + - and *.

Page 8: Code-Tuning By Jacob Shattuck. Code size/complexity vs computation resource utilization A classic example: Bubblesort A classic example: Bubblesort const

Code-Tuning ChecklistCode-Tuning Checklist Would changing the program’s Would changing the program’s

requirements help the program run requirements help the program run faster? (ie: ask for array size instead of faster? (ie: ask for array size instead of trying to find a marker at the array’s end).trying to find a marker at the array’s end).

Have you considered improving Have you considered improving performance by modifying the program’s performance by modifying the program’s design?design?

Have you considered improving Have you considered improving performance by modifying the class performance by modifying the class design? (multiple inheritance will take design? (multiple inheritance will take longer.)longer.)

Page 9: Code-Tuning By Jacob Shattuck. Code size/complexity vs computation resource utilization A classic example: Bubblesort A classic example: Bubblesort const

Know your language!Know your language!

Ex: Short circuit testingEx: Short circuit testingif ( 5 < x ) and ( x < 10 ) then ...if ( 5 < x ) and ( x < 10 ) then ...

is turned intois turned into

if ( 5 < x ) thenif ( 5 < x ) then

if ( 5 < 10 ) then ...if ( 5 < 10 ) then ...

Built into C++ with standard checking, Java can also have short Built into C++ with standard checking, Java can also have short circuit testing.circuit testing.

Page 10: Code-Tuning By Jacob Shattuck. Code size/complexity vs computation resource utilization A classic example: Bubblesort A classic example: Bubblesort const

Short circuiting Short circuiting continuedcontinued

Problems?Problems?

negativeInputFound = false;for ( int i = 0 ; i < count ; i ++ ) {

if ( input[ i ] < 0 ) {negativeInputFound = true;

}}

Page 11: Code-Tuning By Jacob Shattuck. Code size/complexity vs computation resource utilization A classic example: Bubblesort A classic example: Bubblesort const

Short circuiting – the last Short circuiting – the last oneone

Solutions!Solutions! Add a break Add a break

statementstatement If your language If your language

doesn’t have a break doesn’t have a break statement, emulate a statement, emulate a break statement with break statement with a goto.a goto.

Change the for loop Change the for loop to a while loopto a while loop

negativeInputFound = false;for ( int i = 0 ; i < count ; i ++ ) {

if ( input[ i ] < 0 ) {negativeInputFound = true;

}}

Page 12: Code-Tuning By Jacob Shattuck. Code size/complexity vs computation resource utilization A classic example: Bubblesort A classic example: Bubblesort const

Checklist #2Checklist #2 Substitute complicated logic for table Substitute complicated logic for table

lookups (add heuristics).lookups (add heuristics). Jam (combine) loops (when you can)Jam (combine) loops (when you can) Use integer instead of floating pointUse integer instead of floating point Initialize data at compile timeInitialize data at compile time Use constants of the correct typeUse constants of the correct type Precompute results (heuristics)Precompute results (heuristics) Perform common subexpressions once if Perform common subexpressions once if

possiblepossible Translate key routines into low-level Translate key routines into low-level

languagelanguage

Page 13: Code-Tuning By Jacob Shattuck. Code size/complexity vs computation resource utilization A classic example: Bubblesort A classic example: Bubblesort const

The final stepThe final step

As mentioned before… Assembly is As mentioned before… Assembly is faster than C++, C#, or Java.faster than C++, C#, or Java.