Improving Matlab Performance CS1114

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 1 Engineering Problem Solving.
Advertisements

Code Tuning Strategies and Techniques
Introduction to MATLAB The language of Technical Computing.
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Chapter Chapter 4. Think back to any very difficult quantitative problem that you had to solve in some science class How long did it take? How many times.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
Arrays  An array is a collection of like elements.  There are many engineering applications that use arrays.  MATLAB ® stores data in arrays and performs.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine.
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine.
General Computer Science for Engineers CISC 106 Final Exam Review Dr. John Cavazos Computer and Information Sciences 05/18/2009.
CMPUT 101 Lab #6 October 29, :00 – 17:00. Array in C/C++ Array is a structure type variable. One dimension of array int: int num[3]; There are.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/30/2009.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Copyright © 2006 by The McGraw-Hill Companies,
Code-Tuning By Jacob Shattuck. Code size/complexity vs computation resource utilization A classic example: Bubblesort A classic example: Bubblesort const.
1 Chapter-01 Introduction to Computers and C++ Programming.
© 2004 The MathWorks, Inc. 1 MATLAB for C/C++ Programmers Support your C/C++ development using MATLAB’s prebuilt graphics functions and trusted numerics.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
1 © 2012 The MathWorks, Inc. Speeding up MATLAB Applications.
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Improving perfomance Matlab’s a pig. Outline Announcements: –Homework I: Solutions on web –Homework II: on web--due Wed. Homework I Performance Issues.
Which Language is Better?
Eng Ship Structures 1 Introduction to Matlab.
Tot 15 LTPDA Graphic User Interface summary and status N. Tateo 26/06/2007.
Loops CS 103 February 19, 2007 Presented by Nate.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
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.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Data Structures and Algorithms Lecture 3 Instructor: Quratulain Date: 8 th September, 2009.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
ITC Research Computing Support Using Matlab Effectively By: Ed Hall Research Computing Support Center Phone: Φ Fax:
Working with Arrays in MATLAB
Finding Red Pixels – Part 1 Prof. Noah Snavely CS1114
FOUNDATION IN INFORMATION TECHNOLOGY (CS-T-101) TOPIC : INFORMATION SYSTEM – SOFTWARE.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
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.
Computing Systems & Programming ECE Fundamental Concepts Chapter 1 Engineering Problem Solving.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Matlab Programming for Engineers
Digital Image Processing Introduction to M-function Programming.
Georgia Institute of Technology Speed part 4 Barb Ericson Georgia Institute of Technology May 2006.
CIS 601 Fall 2003 Introduction to MATLAB Longin Jan Latecki Based on the lectures of Rolf Lakaemper and David Young.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Arrays.
CSE 455 : Computer Vision MATLAB 101 Getting Started with MATLAB.
Improving performance Matlab’s a pig. Outline Announcements: –Homework I: Solutions on web –Homework II: on web--due Wed. Homework I Performance Issues.
Intro to Matlab Yipei Wang & Qihui Li {yipeiw,
CIS 595 MATLAB First Impressions. MATLAB This introduction will give Some basic ideas Main advantages and drawbacks compared to other languages.
CSC 212 – Data Structures Lecture 15: Big-Oh Notation.
Introduction to Computer Programming using Fortran 77.
Software Engineering Algorithms, Compilers, & Lifecycle.
I/O Streams File I/O 2-D array review
Writing Better R Code WIM Oct 30, 2015 Hui Zhang Research Analytics
Matlab Workshop 9/22/2018.
TRANSLATORS AND IDEs Key Revision Points.
Introduction to MATLAB
StatLab Matlab Workshop
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Loop Statements & Vectorizing Code
Multidimensional Arrays
PROGRAMMING FUNDAMENTALS Lecture # 03. Programming Language A Programming language used to write computer programs. Its mean of communication between.
Simulation And Modeling
Loop Statements & Vectorizing Code
1.3.7 High- and low-level languages and their translators
Presentation transcript:

Improving Matlab Performance CS1114

MATLAB and Speed  MATLAB is an interpreted language  Each instruction by the user takes time to decode into machine code  Proper coding techniques can minimize this decode time and maximize program speed 2

Preallocation of arrays  MATLAB allows growing arrays in loops, but this is inefficient 3 3 array1 = []; array2 = []; n = 1000 ; for i=1:n array1 = [array1 5*i] ; array2(i) = 5*i ; end

Preallocate, using correct data type  For double array,  For another data type, like int8,  Do not use  Avoid changing data type of a declared variable  Instead, have different variable preallocated in correct type 4 array1 = zeros(1,n); array1 = zeros(1,n, ‘int8’); array1 = uint8(zeros(1,n));

Preallocation demonstrated 5

Optimizing Functions  Functions with the same in input vs output arguments operate in-place –Standard function declaration: –In-place function declaration  Best when only outputs are inputs  Must call function with same input/output 6 function y = myfunc (x) function [a b c] = myfunc (x) function x = myfunc (x) function [x b c] = myfunc (x)

In-Place Demonstration 7

Compiling functions  MATLAB does contain a compiler, which turn MATLAB files into executables  It allows you to run MATLAB programs on a computer without MATLAB  In general, doing this will not make your code faster 8

Vectorization  An example of for loop code  Vectorized:  Most built-in functions accept arrays 9 i = 0; for t = 0:.01:10 i = i + 1; y(i) = sin(t); end t = 0:.01:10; y = sin(t);

Logical Indexing  Allows simultaneously indexing all values of an array that meet certain logical criterion  For example, to create an array B which contains all entries in A with value less than 2:  This is one of the most powerful tools in MATLAB, but can be difficult to learn 10 B = A(A < 2);

Vectorization & Logical Indexing Examples 11

repmat and reshape  What if we want to create a matrix whose columns are all of the (x,y) locations in an image? 12 img = rand(3,3); % want to create the matrix % [ ; % ];

repmat and reshape  What if we want to create a matrix whose columns are all of the (x,y) locations in an image? Could use a nested for loop: 13 [rows, cols] = size(img); locs = zeros(2, rows * cols); i = 1; for y = 1:rows for x = 1:cols i = i + 1; locs(:,i) = [x y]’; end Very slow!

repmat and reshape  Instead can use repmat  Replicates a matrix 14 >> help repmat repmat Replicate and tile an array. B = repmat(A,M,N) creates a large matrix B consisting of an M-by-N tiling of copies of A. The size of B is [size(A,1)*M, size(A,2)*N].

repmat and reshape  Instead can use repmat  Replicates a matrix: 15 [rows, cols] = size(img); colidxs = repmat([1:cols], 1, rows); % getting the right y coordinates is trickier rowidxs = reshape(repmat([1:rows], cols, 1),... 1, rows * cols); idxs = [ rows; cols ]; % vectorized matlab code can become hard to understand

repmat and reshape  Not the only (or necessarily the fastest) way to solve this problem  In Matlab, there are often many ways to solve the same problem –Some fast, some slow –Some easy to code, others extremely hard 16

Still not fast enough?  Use a compiled language  C/C++ are known for their speed  C code can be compiled within MATLAB  This will not be needed for your final projects 17

Questions?  /html/speedup.html /html/speedup.html  niques-for-improving-performance.html niques-for-improving-performance.html  operations-on-data/ operations-on-data/ 18