Professional Documents
Culture Documents
Efficiency Notes
Efficiency Notes
BIGIDEA.IT
1. No redundant work
2 . No Duplication
3. Big -0 Notation
4. Comparing Rate of Growth
5. Running time
i.
NoRedundantWorU_ Both same code but
,
② is mud faster
than ① ! !
÷\ interested in this ! ! !
:}
0
*
Opportunistic :
stopÉ we know
number is not prime
ii. NoDup1ication_
→ should not repeat work that has been done before
e. g- Fibonacci
looks more elegant , but many
¥.i:÷÷÷÷¥"¥
duplication :
fib (7)
\
fib / 6) fibls)
fibls)
" "
fib (4)
"
fib (3)
iii. Big-ONotation_ Crate of growth of a function)
"
compares with input n
e. g. 0 Cn)
↳ removes
any addition / multiplications
↳ takes
onI highest rate of growth
e. g. 0 ( F- +1000N ) '
-
n
=
0 (n " )
↳ compare between
is -
prime
"
slow :O Cn
) fast :O ( Tn)
comparingkateofGrowthfcnlvsg.cn
iv.
e.g.
f- ( )
"
gcn) -2
"
n or
- -
n -
: once n > 2 ,
fcn) grows faster inde-atey.i.fm grows faster
see properly !
EXAMPLE G)
-
at j -1 , iterate j -
l times
each time it iterates once in
at j -2 iterate
the main loop, max will iterate , j -2 times
i
call j decreases , compute sum :
each time we max
,
fewer step
so loop in max takes one
-2
,
(j -
1) =
0-11+2 . .
.
-1 (n 3) + In-2)
-
each time
)
.
=
cn¥nn
O Cri) RAP series
}
=
goes through this list
-
j -1 times
# Fibonacci
2) ,
TCn-4.JO#Tln)LTcn-DtTcnt)
constant running time
We know that Tcn )
-1 >
Ican)
,
any;D →
Ten) I 2 Ten -
1)
T d
-
: Tln) 2 2 Tcn -
1) 2 4 Ten -
2) 28 Ten -
3) 2 . . .
2^-11-11) 20 ( I 2h)
I
012 )
"
i. Tcn) =
* exponential ,
much slower than
iterative version
A- collatz
Run time will be unknown
e-
terminates .