In this article, we are going to develop a simple Project of Python Time Complexity Calculator. You can learn what is complexity and what is time complexity here. So, what is Time complexity? Time Complexity of an algorithm is calculated by counting the number of basic operations executed by the algorithm, assuming that each elementary operation takes a constant amount of time to execute.
Python Time Complexity Calculator
We will be using the big-o-calculator library for estimating the temporal or time complexity of sorting methods in Python. If the big-o-calculator is not installed in your python library, first of all, install it.
pip install big-o-calculator
You may evaluate time complexity, compute runtime, and compare two sorting algorithms with this big-o-calculator but sometimes results may vary.
There are a lot of methods available in the bio-o-calculator library, basically, it is a calculator to estimate the time complexity of a sorting function. Some of the important methods of bio-o-calculator library are test(), test_all(), runtime(), compare(),etc. All these methods contain many parameters. Now, let’s see Python Time Complexity Calculator code now.
from bigO import BigO from bigO import algorithm from random import randint fname=input("Enter algorithm name:") def fname(array): # in-place, unstable ''' Best : O(n^2) Time | O(1) Space Average : O(n^2) Time | O(1) Space Worst : O(n^2) Time | O(1) Space ''' currentIndex = 0 while currentIndex < len(array) - 1: smallestIndex = currentIndex for i in range(currentIndex + 1, len(array)): if array[smallestIndex] > array[i]: smallestIdx = i array[currentIndex], array[smallestIndex] = array[smallestIndex], array[currentIndex] currentIndex += 1 return array def TimeComplexity(functionname): lib = BigO() cmplx = lib.test(functionname, "random") cmplx = lib.test(functionname, "sorted") cmplx = lib.test(functionname, "reversed") cmplx = lib.test(functionname, "partial") cmplx = lib.test(functionname, "Ksorted") TimeComplexity(fname)
We are using the Selection Sort algorithm to understand the working of the big-o-calculator library of python.
For calculating the time complexity, you just need to enter the algorithm name and then define the function for this particular algorithm as we did with selectionSort in the example above. After that, we just test the algorithm with the help of the test() function of BigO and it will show all the results. You can test it with other algorithms as well.
Comparison of two algorithms
You can also compare two algorithms. For comparison, we need two algorithms. So, one we are taking bubble sort and second quick sort. Now, let’s see the Python Time Complexity Calculator code for a comparison of two algorithms.
from bigO import BigO from bigO import algorithm from random import randint fname=input("Enter algorithm name:") fname1=input("Enter algorithm name") def fname(array): #bubbleSort algorithm """ Best:O(n) time| O(1) space Average: O(n^2) time | O(1) space Worst: O(n^2) time | O(1) space """ n = len(array) # Traverse through all array elements for i in range(n): # Last i elements are already in place for j in range(0, n-i-1): # traverse the array from 0 to n-i-1 # Swap if the element found is greater # than check the next element if array[j] > array[j+1]: array[j], array[j+1] = array[j+1], array[j] def fname1(array): # in-place | not-stable quicksort algorithm """ Best : O(nlogn) Time | O(logn) Space Average : O(nlogn) Time | O(logn) Space Worst : O(n^2) Time | O(logn) Space """ if len(array) <= 1: return array smaller, equal, larger = , ,  #taking 3 arrays small, equal and large pivot = array[randint(0, len(array) - 1)] #finding the pivot element for x in array: #traverse each element of array if x < pivot: smaller.append(x) #if x element is smaller than pivot element then move it in small array elif x == pivot: equal.append(x) #if x element is equal to pivot element then move it in equal array else: larger.append(x) # if x element is equal to pivot element then move it in large array return fname1(smaller) + equal + fname1(larger) #now again def TimeComplexity(functionname,functionname1): lib = BigO() cmplx = lib.test(functionname, "random") cmplx = lib.test(functionname, "sorted") cmplx = lib.test(functionname, "reversed") cmplx = lib.test(functionname1, "random") cmplx = lib.test(functionname1, "sorted") cmplx = lib.test(functionname1, "reversed") cmplx=lib.compare(fname,fname1,"random",5000) cmplx=lib.compare(fname,fname1,"all",5000) TimeComplexity(fname,fname1)
For comparing two algorithms we are using compare method of BigO.
- fname is for bubbleSort algorithm and fname1 is for quickSort.
- Select a random array with size 5000.
- The second time we compare all types of arrays and the size is 5000 in all cases.
Using the Python big-o-calculator library and some code, we have constructed a simple python time complexity calculator that allows us to simply compute the time complexity for different algorithms.
Thank you for reading this article, click here to start learning Python in 2022.
- Free Python Certification course from Alison: Good for Resume
- Download 1000+ Projects, All B.Tech & Programming Notes, Job, Resume & Interview Guide, and More – Get Your Ultimate Programming Bundle!
- Top 5 Websites to Learn Programming in 2024
- Python Internship for college students and freshers: Apply Here
- New secrets to Earn money with Python in 2024
- Connect with HR Directly – Job Hack
- Google offering free Python course: Enroll Today
- Google Internship 2024
- TCS Launched Free Certification Course with Industry Recognized Value
- What is web development for beginners?
- Microsoft Giving Free Machine Learning Course: Enroll Now
- Accenture Giving Free Developer Certificate in 2023
- Amazon Summer Internship 2023
- Amazon Giving Free Machine Learning Course with Certificate: Enroll Now
- Google Summer Internship 2023
- 5 Secret ChatGPT skills to make money
- Free Google Certification Courses
- 5 AI tools for coders better than ChatGPT
- New secrets to Earn money with Python in 2023
- Music Recommendation System in Machine Learning
- How to utilize ChatGPT to improve your coding skills?
- Create your own ChatGPT with Python
- Bakery Management System in Python | Class 12 Project
- SQLite | CRUD Operations in Python
- Event Management System Project in Python
- Ticket Booking and Management in Python
- Hostel Management System Project in Python
- Sales Management System Project in Python
- Bank Management System Project in C++
- Python Download File from URL | 4 Methods