Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.

Slides:



Advertisements
Similar presentations
Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University
Advertisements

Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Practice Quiz Question
ALG0183 Algorithms & Data Structures Lecture 3 Algorithm Analysis 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Weiss Chapter 5 Sahni.
LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.
Computer Science 1620 Loops.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Complexity Analysis (Part I)
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Complexity Analysis (Part I)
Copyright © 1998 Wanda Kunkle Computer Organization 1 Chapter 2.1 Introduction.
Computer Science 1620 Programming & Problem Solving.
Unit 171 Algorithms and Problem Solving - II Algorithm Efficiency Primality Testing Improved Primality Testing Sieve of Eratosthenes Primality Testing.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Spring2012 Lecture#10 CSE 246 Data Structures and Algorithms.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Chapter 5: Control Structures II (Repetition)
Data Structures & AlgorithmsIT 0501 Algorithm Analysis I.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Lecture 2 Computational Complexity
Lecture 2b: Performance Metrics. Performance Metrics Measurable characteristics of a computer system: Count of an event Duration of a time interval Size.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Analysis of Algorithms
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
First steps Jordi Cortadella Department of Computer Science.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
1 CS161 Introduction to Computer Science Topic #9.
Algorithm Analysis Part of slides are borrowed from UST.
Overview Go over parts of quiz? Another iteration structure for loop.
Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Algorithm Analysis (Big O)
Computer Organization Instruction Set Architecture (ISA) Instruction Set Architecture (ISA), or simply Architecture, of a computer is the.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Spring 2006CISC101 - Prof. McLeod1 Announcements Assn 4 is posted. Note that due date is the 12 th (Monday) at 7pm. (Last assignment!) Final Exam on June.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Program Performance 황승원 Fall 2010 CSE, POSTECH. Publishing Hwang’s Algorithm Hwang’s took only 0.1 sec for DATASET1 in her PC while Dijkstra’s took 0.2.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 3. Time Complexity Calculations.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Complexity Analysis (Part I)
Repetitive Structures
Introduction to Analysis of Algorithms
Lecture 3 of Computer Science II
Computing Fundamentals
Control Structures Part 1
Complexity Analysis (Part I)
Complexity Analysis (Part I)
Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases.
Presentation transcript:

Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay Lecture 7, Efficiency of programs Thursday 1 July 2010

Overview  Efficiency of programs Notion of time complexity  Example Estimating value of PI Calculating terms of Fibonacci series [Some slides courtesy Prof Milind Sohoni]

Computational time  Although the computer works very fast, it does take a finite amount of time for every computation  If we do not carefully design our program, we may force the machine to do many more computations than necessary to solve the problem  The order of magnitude of time required to execute any program is called its “Time Complexity”  We should design our algorithm (steps in our program) such that the execution time is minimized

Example

Example …

Estimating value of PI #include using namespace std; // estimate pi int main(){ float pi; int i, j, N, count; cout << “Give value of N\n”; cin >> N;

Estimating PI … count=0; for (i=1;i<=N;i=i+1){ for (j=1;j<=N;j=j+1){ if (i*i+j*j<=N*N) count=count+1; }; pi=4.0*count/(N*N); cout << pi << “\n”; return 0; }

Estimating PI … count=0; for (i=1;i<=N;i=i+1){ for (j=1;j<=N;j=j+1){ if (i*i+j*j<=N*N) count=count+1; }; pi=4.0*count/(N*N); cout << pi << “\n”; return 0; }

Quiz Q. We have declared our variables i, j, N as integers, the effect on the estimation of Pi using pi=4.0*count/(N*N); will be A.Negligible, we have declared pi as float B.Very large, because the division operation in the final formula [count/(N*N)] is of the type integer divided by integer C.Very large because the values of some terms may be beyond the limits of integer representation D.None of these

Modified program version 1 #include using namespace std; // estimate pi int main() { float pi; float i,j,N; long count; cout << "N? " ; cin >> N;

Modified program version 1 … count=0; for (i=1;i<=N;i=i+1){ for (j=1;j<=N;j=j+1){ if (i*i+j*j<=N*N) count=count+1; }; pi=4.0*count/(N*N); cout << "Value of Pi is: " << pi << "\n"; return 0; }

Execution results cpp]$ c++ calculatepi.cpp -o pi cpp]$./pi N? 1000 Value of Pi is: cpp]$./pi N? Value of Pi is:

Determining computational efficiency  Having ensured that our program works correctly, we now wish to determine how long it takes to run the program for different values of N  we want to find the execution time using some utility program which internally keeps time while the Operating System (OS) runs our program  UNIX OS and its variants provide a utility program called ‘time’ ALL OS’s provide such utilities Special software used for simulating large number of concurrent users Loadrunner, J-meter,...

Measuring execution time  We can use this command to run our program under its control cpp]$ time./pi N? Value of Pi is: real 0m3.690s user 0m1.076s sys 0m0.012s  What do these different values signify?

Explanations  Real time means the total clock time which the program took to execute from start to finish Includes time spent by us in giving input values  user time and sys (system) time are two independent time counts User time is actually the computing time taken by the computer to execute the program Sys time is the time which was spent by the supervising OS for executing our program  User time represents our algorithmic time complexity

Execution results cpp]$ c++ calculatepi.cpp -o pi cpp]$ time./pi N? 1000 Value of Pi is: real 0m2.496s user 0m0.000s sys 0m0.000s cpp]$ time./pi N? Value of Pi is: real 0m3.690s user 0m1.076s sys 0m0.012s

Execution results … cpp]$ time./pi N? Value of Pi is: real 0m7.181s user 0m4.304s sys 0m0.000s cpp]$ time./pi N? Value of Pi is: real 0m29.742s user 0m26.714s sys 0m0.000s cpp]$

Further reduction in execution time  We observe that the major computation is happening during evaluation of if condition  if (i*i+j*j<=N*N) count=count+1  This computation is done within a nested iteration each running N times. (so N 2 times)  We notice that while the values of i and j are changing every time, that of N is fixed during all iterations So why calculate N*N again and again?

Modified program version 2 … int n2 = N*N; count=0; for (i=1;i<=N;i=i+1){ for (j=1;j<=N;j=j+1){ if (i*i+j*j<=n2) count=count+1; }; pi=4.0*count/n2;

Execution results … cpp]$ c++ calculatepiv2.cpp -o v2 cpp]$ time./v2 N? Value of Pi is: real 0m7.821s user 0m4.308s sys 0m0.012s cpp]$ time./v2 N? Value of Pi is: real 0m31.313s user 0m26.898s No appreciable change, why? sys 0m0.012s

Quiz Q. The execution time for each of the two versions is not appreciably different because: A Multiplication does not take very large time, it is the division operation and the addition operation which is time consuming B since i and j are varying, computing i*i, j*j each takes much longer than N*N C Our program somehow figures that N is not changing, so it calculates N*N only once and uses that value repeatedly D I do not know and also cannot guess

Further reduction in execution time  We observe that the major computation is happening during evaluation of if condition if (i*i+j*j<=N*N) count=count+1  This computation is done within a nested iteration, each running N times. (so N 2 times)  We notice that the value of N is fixed during all iterations, so why calculate N*N again and again?  Instead of considering the square, we can consider only a triangle (half of square) with area Pi/8 This will reduce computations by half

Modified program version 3 … n2 = N*N; count=0; for (i=1;i<=N;i=i+1){ for (j=i;j<=N;j=j+1){ if (i*i+j*j<=n2) count=count+1; }; pi=8.0*count/n2; cout << endl << "Value of Pi is: “ cout << pi << "\n"; return 0;

Execution results … cpp]$ time./v3 N? Value of Pi is: real 0m4.301s user 0m2.132s sys 0m0.008s cpp]$ time./v3 N? Value of Pi is: real 0m17.216s user 0m13.461s sys 0m0.012s Appreciable reduction

Time complexity of an algorithm  There are two ways of looking at the time taken by a program to execute.  Micro-view We look at the time taken by each instruction in the program to execute, and the number of times that instruction is executed. While the machine executes instructions in few tens of microseconds, or even in hundreds of nanoseconds, every instruction takes a different amount of time depending upon its nature A ‘unit’ time can be used to represent comparative execution times

Some hypothetical comparative execution times  Assignment 1 unit  Addition/subtraction 2  Multiplication 3  Division 5  Comparison 3  Floating point operation 5 times int  Some other operations which we have not yet studied, such as reading from disk, or assignment to an array element, take longer time Calculating value of index expression, e.g. A[3*i+j]

Time complexity of an algorithm …  If there is an iteration which executes N times, where N is an input value If N largely determines the amount of computations in a program, we call it the ‘size’ of the problem Number of computations are expressed in terms of N. For our Pi calculations, these may be, say, 24 N N  In general aN 2 + bN + c

Time complexity, Macro view  When comparing two algorithms, we can get such expressions for each, and then compare for specific values of N  Consider prog1, and prog2, with time complexity: Prog1: 24 N N Prog2: N  Prog1 will run faster than prog2 up to a certain N (What is that value?)  But for all values of N greater than this threshold, prog2 will always run faster

Time complexity, Macro view …  It is customary to compare algorithms on the basis of their behaviour for very large values of N [ Limit as N infinity]  Thus Prog1 is order N 2, or O(N 2 ) Prog2 is order N, or O(N)  In general, our effort should be Try to reduce the order of complexity Within the same order, try to reduce the coefficients of the expression

THANK YOU