# 22 BigO - 22 - Big-O Notation Motivation This chapter is...

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

BigO - 1 22 - Big-O Notation Motivation This chapter is about measuring the performance of computer programs. Our approach is based on the asymptotic complexity measure. That means that we don’t try to count the exact number of steps in a program, but rather how that number grows with the size of the input to the program. This gives us a measure that will work for different operating systems, compilers and CPUs. The asymptotic complexity is written using big-O notation. We will describe big-O notation, and show how big-O bounds can be computed for some common algorithms. Running Time of Some Algorithms Let’s consider a short piece of MATLAB code: testtime.m %short piece of code x=3 * y+2 ; z=z+1 ; If x,y and z are scalars, this piece of code always takes the same time to run no matter what numbers they are. In terms of actual computer instructions or clock ticks, it’s difficult to say exactly how long it takes. But whatever it is, it should be the same whenever this piece of code is executed. We say it takes constant time, which we write as O(1). The Big “O” stands for “order”. Because O is a constant, we don’t need to be specific as to whether it means computer cycles or instructions, or the time on our watch. O(1) means some constant, it might be 5, or 1 or 1000. Now let’s consider a simple for loop: %testtime2.m %a simple for-loop %E77 reader %7-01 D# function [time] = testtime2(v,N) tic

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

View Full Document
The loop will run exactly N times. No matter what you put in for v, the inside of the loop will take the same time. Because the inside of the loop takes constant time, the total running time is proportional to N. We write it as O(N). The actual number of instructions might be 50N, while the running time might be 17N microseconds. It might even be 17N+3 microseconds because the loop needs some time to start up. The big-O notation allows a multiplication factor (like 17) as well as an additive factor (like 3). As long as it is a linear function which is proportional to N, the correct notation is O(N) and the code is said to have linear running time. More precisely, a function f(N) is said to be O(g(N)) if ) ( ) ( N g k N f for some constant k and all sufficiently large N. The MATLAB command tic starts a stopwatch running. toc returns the time elapsed since tic . The output of two runs of testtime2.m is: >> testtime2(4,10) ans = 0 >> testtime2(4,100) ans = 0.0100 It didn’t take literally 0 time for the first example to run, but it was so fast that the computer’s clock couldn’t catch it. Now let’s look at a more complicated example, a nested
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/12/2011 for the course E 7 taught by Professor Patzek during the Spring '08 term at University of California, Berkeley.

### Page1 / 8

22 BigO - 22 - Big-O Notation Motivation This chapter is...

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

View Full Document
Ask a homework question - tutors are online