0% found this document useful (0 votes)
7 views

Performance Analysis of an Algorithm_0

The document discusses the importance of performance analysis in algorithms, focusing on time and space complexity to evaluate and compare different algorithms. It explains how to determine the efficiency of algorithms based on fixed and variable space and time requirements, providing examples of constant and linear complexities. The analysis aids in selecting the most suitable algorithm for specific requirements in programming and application development.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Performance Analysis of an Algorithm_0

The document discusses the importance of performance analysis in algorithms, focusing on time and space complexity to evaluate and compare different algorithms. It explains how to determine the efficiency of algorithms based on fixed and variable space and time requirements, providing examples of constant and linear complexities. The analysis aids in selecting the most suitable algorithm for specific requirements in programming and application development.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Performance Analysis

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

analysis. perform better for large number of input.


Why we If you are developing a web-based

need to application, additional factors like:


• Bandwidth usage

calculate • Number of simultaneous


connections

an and other factors will also come


into consideration.

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

consider: algorithm, easy to


understand, easy to
implement, etc.,
The 1. Whether that algorithm is
providing the exact solution for the
performance problem?

of an 2. Whether it is easy to understand?


3. Whether it is easy to implement?
algorithm 4. How much space (memory) it
depends on requires to solve the problem?

the following 5. How much time it takes to solve


the problem? Etc.,
elements:
Factors in • When we want to analyze an
algorithm, we consider only the
deciding the space and time required by that
performance particular algorithm and we ignore
all the remaining elements.
of an
algorithm
1. Time complexity: Time
Factors that required to complete the task
of that algorithm (Time
decide the Complexity)
performance 2. Space Complexity: Space
required to complete the task
of an of that algorithm (Space
algorithm: Complexity). It includes
program space and data space.
Based on this information,

Performance performance analysis of an algorithm


can also be defined as follows:

Analysis of • Performance analysis of an


algorithm is the process of

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

• If any algorithm requires a fixed amount of space


for all input values, then that space complexity is
said to be Constant Space Complexity.
Consider the following piece of code:
• Calculate the sum of “n” numbers given in the array.
Explanation

• A variable “total_sum” that takes constant sum of 1


unit
A variable “length” that takes constant sum of 1 unit
A variable “i” that takes constant sum of 1 unit
• But the variable arr[] is an array, it’s space
consumption increases with the increase of input size.
i.e n.
• Hence the space consumption is [3 + n] units.
Linear • If the amount of space required by
an algorithm is increased with the
Space increase of input value, then that
space complexity is said to be
Complexity Linear Space Complexity.
• Every algorithm requires some amount of
computer time to execute its instruction to
What is perform the task.
• This computer time required is called time
Time complexity.
complexity? • Definition: The time complexity of an
algorithm is the total amount of time
required by an algorithm to complete its
execution.
Generally,
the running • Whether it is running on Single processor
machine or Multi processor machine.
time of an • Whether it is a 32 bit machine or 64 bit machine.
algorithm • Read and Write speed of the machine.
depends • The amount of time required by an algorithm to
perform Arithmetic operations, logical operations
upon the , return value and assignment operations etc.,
following: • Input data
Calculating Time Complexity

• Calculating Time Complexity of an algorithm


based on the system configuration is a very
difficult task because the configuration changes
from one system to another system.
• To solve this problem, we must assume a model
machine with a specific configuration.
• So that, we can able to calculate generalized
time complexity according to that model
machine.
To calculate the time complexity of an algorithm,
we need to define a model machine. Let us
assume a machine with following configuration:
• It is a Single processor machine
• It is a 32 bit Operating System machine
Calculating • It performs sequential execution
Time • It requires 1 unit of time for Arithmetic and
Logical operations
Complexity • It requires 1 unit of time for Assignment and
Return value
• It requires 1 unit of time for Read and Write
operations
Sample code:
int sum(int a, int b)
{
return a+b;
}
In the above sample code, it requires 1 unit of time to calculate a+b
and 1 unit of time to return the value.
That means, totally it takes 2 units of time to complete its execution.
And it does not change based on the input values of a and b.
That means for all input values, it requires the same amount of time i.e.
2 units.
Constant • If any program requires a fixed
Time amount of time for all input values,
then its time complexity is said to

Complexity be Constant Time Complexity.


Sample code 2:
• Consider the following code:
Explanation:

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

You might also like