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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Code Tuning Strategies and Techniques
Chapt.2 Machine Architecture Impact of languages –Support – faster, more secure Primitive Operations –e.g. nested subroutine calls »Subroutines implemented.
Code Tuning Techniques CPSC 315 – Programming Studio Fall 2008 Most examples from Code Complete 2.
Tori Bowman CSSE 375, Rose-Hulman October 22, Code Tuning (Chapters of Code Complete)
Code Tuning Strategies and Techniques CS524 – Software Engineering Azusa Pacific University Dr. Sheldon X. Liang Mike Rickman.
CS0007: Introduction to Computer Programming
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
Compiler Construction
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Program Design and Development
Computer Science 1620 Programming & Problem Solving.
Code Tuning Techniques CPSC 315 – Programming Studio Spring 2008 Most examples from Code Complete 2.
Code Tuning Techniques CPSC 315 – Programming Studio Fall 2009 Most examples from Code Complete 2.
C++ Control Flow Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th -22 nd Sept 2006.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
PRE-PROGRAMMING PHASE
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Code Tuning Strategies and Techniques CS480 – Software Engineering II Azusa Pacific University Dr. Sheldon X. Liang.
General Issues in Using Variables
Introduction to FORTRAN-90 University of Liverpool course.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Cosc 2150: Computer Organization
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.
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.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Looping and Counting Lecture 3 Hartmut Kaiser
COMPUTER PROGRAMMING II SUMMER 2011 Visual Basic to C#
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
RUBY by Ryan Chase.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.
Code Generation CPSC 388 Ellen Walker Hiram College.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
COP 3540 Data Structures with OOP
Improving Matlab Performance CS1114
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.
Pointers and Arrays in C and Assembly Language. Arrays Provide useful abstraction Match up to machine memory organization Array index computation –time.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.
Software Engineering Algorithms, Compilers, & Lifecycle.
Flow Control. Comments u Comments: /* This is a comment */ –Use them! –Comments should explain: v special cases v the use of functions (parameters, return.
Optimization. How to Optimize Code Conventional Wisdom: 1.Don't do it 2.(For experts only) Don't do it yet.
ENGINEERING 1D04 Tutorial 1. WELCOME! What we’re doing today Who am I How to do well What are computer programs Software Development Cycle Pseudo Code.
Problem Solving: Brute Force Approaches
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Chap. 6 :: Control Flow Michael L. Scott.
Problem Solving: Brute Force Approaches
Arrays, For loop While loop Do while loop
MSIS 655 Advanced Business Applications Programming
3.4 Computer systems Boolean logic Lesson 2.
Chap. 6 :: Control Flow Michael L. Scott.
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Unit 3: Variables in Java
Controlling Program Flow
Looping Structures.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Code-Tuning By Jacob Shattuck

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)--; }

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

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)

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)

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

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 *.

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.)

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.

Short circuiting continued Problems? negativeInputFound = false; for ( int i = 0 ; i < count ; i ++ ) { if ( input[ i ] < 0 ) { negativeInputFound = true; }

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; }

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

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.