This preview has intentionally blurred sections. Sign up to view the full version.
View Full 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 you’re programming, you ideally want short, elegant programs that run quickly, use very little space in the computer’s memory, and always give the correct answer. In practice, particularly as you’re learning to program, you’ll sometimes write code that runs slowly. There are two main ways to make sure your code runs quickly: 1. Think deeply about the process you’re 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 you’ve chosen that optimizes how each step is implemented in memory and on your physical computer. We’ll first discuss the second of these methods since it’s 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, you’ll often hear that it’s bad to use loops when you could have instead used vector or matrix functions. This is because Matlab’s builtin 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 I’ve given you so far in this course could be written more efficiently using builtin functions and by ‘vectorizing’ (replacing loops with vector operations). I’ve given you many examples using loops for two reasons. First of all, they’re often some of the simplest loops to write, and thus they serve a pedagogical purpose. As you start writing more complicated programs, you’ll 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 skillset 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 you’ve 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, Preallocated Memory Loops, Vector Operations As an example of the huge differences in efficiency between methods in Matlab, let’s compare three different methods of performing the same operation in Matlab. Particularly, let’s create a 10,000 element vector filled with the number 5 ....
View
Full
Document
This note was uploaded on 03/20/2011 for the course ENGINEERIN 127 taught by Professor Finch during the Spring '08 term at Rutgers.
 Spring '08
 Finch

Click to edit the document details