14-440-127+Lecture+10

14-440-127+Lecture+10 - 14:440:127 Introduction to...

Info iconThis preview shows pages 1–2. 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: 14:440:127 Introduction to Computers for Engineers Notes for Lecture 10 Rutgers University, Spring 2010 Instructor- Blase E. Ur 1 What is Efficiency? When youre programming, you ideally want short, elegant programs that run quickly, use very little space in the computers memory, and always give the correct answer. In practice, particularly as youre learning to program, youll sometimes write code that runs slowly. There are two main ways to make sure your code runs quickly: 1. Think deeply about the process youre using the solve a problem and consider whether the number of steps in your process can be reduced. 2. Use small tricks in the programming language youve chosen that optimizes how each step is implemented in memory and on your physical computer. Well first discuss the second of these methods since its much easier, albeit much less effective. The first of these methods is a large part of Computer Science as a discipline. 2 Optimizing Matlab Code 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 so far in this course 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 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 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 first one or two! 2.1 Measuring Efficiency- Tic and Toc Matlab has two convenient commands that let you time how long an operation takes to compute. To start (and reset) a timer, use the command tic; . To stop the timer and display the amount of time elapsed, use toc; . 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 ! 2.2 Loops, Pre-allocated Memory Loops, Vector Operations As an example of the huge differences in efficiency between methods in Matlab, lets compare three different methods of performing the same operation in Matlab. Particularly, lets create a 10,000 element vector filled with the number 5 ....
View Full Document

Page1 / 13

14-440-127+Lecture+10 - 14:440:127 Introduction to...

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

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