Performance Analysis of an Algorithm_0
Performance Analysis of an Algorithm_0
of an Algorithm
Understand the importance
of calculating the
performance of an
algorithm
Objectives:
Describe how space and
time complexity is used to
analyze an algorithm.
• Consider 2 programmers “A” and “B”. Both
Why we of them submits 2 different solutions for
the same program. Now how do we
need to
decide which solution is efficient and
performs better?
• And also A’s program might perform
perform better for small number of input but will
not perform when the input size
algorithm
increases.
• Similarly, B’s program will not perform
when the input size is small but will
algorithm
• In Computer Science, there are
multiple algorithms to solve a problem.
When we have more than one
What is algorithm to solve a problem, we need
to select the best one.
Performance • Performance analysis helps us to select
Analysis of an the best algorithm from multiple
algorithms to solve a problem.
algorithm? • When there are multiple alternative
algorithms to solve a problem, we
analyze them and pick the one which is
best suitable for our requirements.
Formal definitions:
• Performance of an algorithm is a
What is process of making evaluative
Performance judgement about algorithms.
• Performance of an algorithm
Analysis of an means predicting the resources
algorithm? which are required to an algorithm
to perform its task.
• To compare algorithms, we
Factors use a set of parameters or set
of elements like memory
to required by that algorithm,
the execution speed of that
an Algorithm
calculating space and time
required by that algorithm.
When we design an algorithm to
solve a problem, it needs some
computer memory to complete its
execution. For any algorithm,
What is memory is required for the following
purposes:
space • To store program instructions.
complexity? • To store constant values.
• To store variable values.
• And for few other things like
function calls, jumping statements
etc,.
• Space complexity is about
calculating the amount of space
What is consumed by algorithm during
its execution.
space
• Space Complexity is the total
complexity? amount of computer memory
required by an algorithm to
complete its execution is called
as space complexity of that
algorithm.
1. Fixed or
constant space
Two types of complexity
Space
Complexity: 2. Linear space
complexity
(Variable space
complexity)
Fixed or constant space
complexity
• To calculate the space complexity, we must know
the memory required to store different datatype
values (according to the compiler).
• For example, the C Programming Language
compiler requires the following:
1. 2 bytes to store Integer value.
2. 4 bytes to store Floating Point value.
3. 1 byte to store Character value.
4. 6 (OR) 8 bytes to store double value.
Consider the following piece of code:
• Find the square of a number: (sample code)
int square(int a)
{
return a*a;
}
Explanation:
• In the example code, it requires 2 bytes of memory to store
variable 'a' and another 2 bytes of memory is used for return value.
• That means, totally it requires 4 bytes of memory to complete its
execution.
• And this 4 bytes of memory is fixed for any input value of 'a’.
• Here we can solve the problem without consuming any extra space,
hence the space complexity is constant even if the input size
increases.
• This space complexity is said to be Constant Space Complexity.
Constant Space Complexity
Repetition
• Cost is the amount of computer
time required for a single
operation in each line.
• Repetition is the amount of
computer time required by each
Explanation: operation for all its repetitions.
• Total is the amount of computer
time required by each operation to
execute.
• So sample code 2, requires '4n+4'
Units of computer time to
complete the task. Here the exact
time is not fixed.
• And it changes based on
the n value. If we increase
Explanation: the n value, then the time required
also increases linearly.
• Totally it takes '4n+4' units of time
to complete its execution and it
is Linear Time Complexity.
Linear • If the amount of time required by
an algorithm is increased with the
Time increase of input value, then that
time complexity is said to be Linear
Complexity Time Complexity.
Activity