14-440-127+Lecture+10

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

This preview shows pages 1–2. Sign up to view the full content.

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

View Full Document
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 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 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 I’ve given you so far in this course could be written more efficiently using built-in 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 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 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, Pre-allocated 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.

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
Ask a homework question - tutors are online