lec-2 of data stc
TRANSCRIPT
-
8/9/2019 Lec-2 of data stc
1/13
Questions that will be answered
1/31
What is a good or "efficient" program?
How to measure the efficiency of a program?
How to analyse a simple program?
How to compare different programs?
What is the big! notation?
What is the impact of input on program performance?
What are the standard program analysis techniues?
#o we need fast machines or fast algorithms?
-
8/9/2019 Lec-2 of data stc
2/13
Which is better?
$/31
The running time of a program.
%rogram easy to understand?
%rogram easy to code and debug?
%rogram ma&ing efficient use of resources?
%rogram running as fast as possible?
Ways of measuring efficiency:
how long it ta&eshow much memory it uses
-
8/9/2019 Lec-2 of data stc
3/13
Measuring Efciency
3/31
Want to achieve platform-independence
'se an abstract machine that usesstepsof time and units
of memory( instead of seconds or bytes
each elementary operation ta&es 1 step
each elementary instance occupies 1 unit of memory
-
8/9/2019 Lec-2 of data stc
4/13
-
8/9/2019 Lec-2 of data stc
5/13
Analysis o Sum
*/31
1+, #escribe the si-e of the input in terms of one or more
parameters.
nput to Sumis an array of 0 integers( so si-e is 0+
$+, hen( count how many steps are used for an input of that si-e. 2 step is an elementary operation such as
( 4( 5( 26i7
2 8imple 9:ample
-
8/9/2019 Lec-2 of data stc
6/13
Analysis o Sum(!
;/31
// Input: int A[N], array of N integers
// Output: Sum of all numbers in array A
int Sum(int A[], int N {
int s=!
for (int i=! i" N! i##)
s = s # A[i]!
return s!
$
1
$ 3 )
*; aluate each le>el of
nesting as needed.
int %,&!
for (%=! %"N! %##) for (&=N! &'! &)
sum #= %!
8tart with outer loop.
How many iterations? 0
How much time per iteration? 0eed to e>aluate inner loop
nner loop uses !C0, time
otal time is 0 F !C0, 5 !C0F0, 5 !C0$,
8tandard 2nalysis echniues
%!#'A 'E9
-
8/9/2019 Lec-2 of data stc
13/13
Se+uence o Statements
13/31
Ior a seuence of statements( compute their comple:ity functions
indi>idually and add them up
for (%=! % " N! %##)
for (& =! & " %! #) sum = sum # %&!
for (l=! l " N! l##)
sum = sum l!
printf(*sum is no+ f*, sum)!
!C0$,
!C0,
!C1,
otal cost is !C0$, !C0, !C1, 5 !C0$,
8'D 'E9
8tandard 2nalysis echniues