LEC08+Notes (3) - 1 14:440:127 Introduction to Computers...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 14:440:127 Introduction to Computers for Engineers Notes for Lecture 08 Rutgers University, Fall 2010 Instructor- Brenda V. Cortez 1 Finishing Up Loops - Efficiency Concerns In Matlab, youll often hear that its bad to use loops when you could have instead used vector or matrix functions. This is because Matlabs built-in functions and arithmetic operations are optimized to use data stored as vectors or matrices in memory. Thus, whenever possible, use vectors, matrices, and their associated functions. Many of the examples Ive given you in the past two lectures and in the first part of these notes could be written more efficiently using built- in functions and by vectorizing (replacing loops with vector operations). Ive given you many examples using loops for two reasons. First of all, theyre often some of the simplest loops to write, and thus they serve a pedagogical(i.e. of or relating to teaching.) purpose. As you start writing more complicated programs, youll often need to write loops, but these loops will be implementing much more complex algorithms . Second, most computer programming languages (C, C++, Java...) use loops much more extensively than Matlab, so being good at loops is a skill-set that you can port to other languages you learn. As with natural (human) languages, learning more programming languages becomes easier after your rst one or two! 1.1 Efficiency 1.1.1 Time it - Tic; and Toc; During class I did an example using the command clock along with command etime as well as the commands tic; and toc; . However, when you want to time how long an operation takes to compute, use the tic; and toc; commands. To start (and reset) a timer, use the tic; command. To stop the timer and display the amount of time elapsed, use the toc; command. If youve brainstormed a few different methods for solving a problem and want to have an idea of which method runs most quickly in Matlab, use tic and toc! tic; x = [32 3 45 56 76 24 56 78 65 43 43 54 78 35]; total=0; for k = 1:length(x) total = total + x(k); end disp(total) toc; 2 The message you will get from using tic; and toc; is: Elapsed time is 0.000503 seconds. 1.2 Vectorizing Code As stated above, when possible, vectorize (use vertors and vector functions) your code to increase efficiency. For example, lets vectorize the code above and time its execution time. tic; x = [32 3 45 56 76 24 56 78 65 43 43 54 78 35]; total = sum(x); disp(total) toc; Elapsed time is 0.000068 seconds. As you can see, there is a great improvement in execution time, which went from 0.000503 sec- onds using a loop to 0.000068 by vectorizing the code! The vector x is small and execution time can already be improved, so imagine the impact of this when working with large data sets!...
View Full Document

This note was uploaded on 03/24/2011 for the course ENGINEERIN 127 taught by Professor Finch during the Spring '08 term at Rutgers.

Page1 / 22

LEC08+Notes (3) - 1 14:440:127 Introduction to Computers...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online