Performance Analysis - Time and Space Complexity
π Performance Analysis
-
Time Complexity — How much time (number of operations) the algorithm takes.
-
Space Complexity — How much memory (space) the algorithm uses.
π 1. Time Complexity
-
Measures the amount of time an algorithm takes to complete.
-
It is expressed as a function of the input size
n
. -
We usually use Big-O notation (e.g., O(n), O(log n), O(n²)) to express it.
Example for Time Complexity:
Suppose we have a simple loop:
-
The loop runs n times.
-
Each
printf
is a constant-time operation. -
So, Time Complexity = O(n) (linear time).
Types of Common Time Complexities:
Complexity | Name | Example |
---|---|---|
O(1) | Constant time | Accessing array element |
O(log n) | Logarithmic time | Binary search |
O(n) | Linear time | Traversing an array |
O(n log n) | Linearithmic time | Merge sort, Heap sort |
O(n²) | Quadratic time | Bubble sort, Selection sort |
O(2βΏ) | Exponential time | Solving Tower of Hanoi |
π 2. Space Complexity
-
Measures the amount of memory an algorithm needs to complete its execution.
-
Memory includes:
-
Variables
-
Data structures (arrays, linked lists)
-
Recursion stack space
-
Example for Space Complexity:
Suppose:
-
These use constant memory → Space Complexity = O(1).
Now:
-
Memory used grows with n → Space Complexity = O(n).
π Example Combining Both:
Suppose we want to find the sum of all elements in an array:
-
Time Complexity:The loop runs n times → O(n).
-
Space Complexity:Only
total
variable used (ignoring input array) → O(1).
π― Summary Table
Aspect | Description | Example |
---|---|---|
Time Complexity | How long an algorithm runs | O(n), O(log n), O(n²) |
Space Complexity | How much memory is needed | O(1), O(n) |
Performance Analysis | Study of time and space needs | Done before execution |
π Why Performance Analysis is Important?
-
Helps to choose the best algorithm.
-
Helps to predict performance for large inputs.
-
Avoids wasting time, memory, and power.
π Simple C Program to Demonstrate Time Complexity
We'll create a program to traverse an array — this has O(n) time complexity.
We'll also measure the execution time using <time.h>
functions.
π Explanation
-
clock_t start, end;
— to record start and end time. -
clock()
— function returns processor time consumed so far. -
The
for
loop is O(n) — it depends linearly on the sizen
. -
We find the difference between
start
andend
to measure how long it took.
✅ You can enter a large value of n
(like 1000000) and see how time increases!
π C Program for Bubble Sort with Time Measurement
π Explanation
-
Outer loop: Runs
n-1
times. -
Inner loop: On average
n/2
times for each pass. -
Thus, total comparisons ≈ n²/2 ⇒ O(n²) time complexity.
-
If
n = 10000
, it will take a longer time compared to simple array traversal (O(n))!
π Key Observations
Complexity Program Time taken O(n) Array Traversal (Sum) Very fast (even for large n) O(n²) Bubble Sort Very slow for large n (say n > 5000) |
---|
- Bubble Sort is inefficient when
n
is large because of its quadratic nature.
-
That’s why better sorting algorithms like Merge Sort (O(n log n)) are preferred.
⚡ Summary
-
O(n) — grows linearly, fast for large inputs.
-
O(n²) — grows quadratically, very slow when
n
increases. -
Measuring time helps in understanding real performance, not just theoretical Big-O.
Comments
Post a Comment