Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a tool to automatically calculate Big-O complexity for a function [duplicate]

While studying algorithms and data structures I manually evaluate BigO complexity for my script. Is there a way, let say a button in any Python IDE or a package, to calculate BigO for any given function or a program?

UPDATE:

Let's say I have

def print_first_element(a):
    print a[0]

why I can't write the analyzer which will say me ok you access the array (list) by index and its O(1), or

def print_all_element_of_list(a):
    for i in a:
        print i

ok you have full scan so the complexity is O(n)

and so forth

like image 432
Igor Barinov Avatar asked Jul 08 '15 06:07

Igor Barinov


People also ask

Is there a Python package that calculate time complexity?

big_O is a Python module to estimate the time complexity of Python code from its execution time. It can be used to analyze how functions scale with inputs of increasing size.

What is the easiest way to calculate time complexity?

Let's use T(n) as the total time in function of the input size n , and t as the time complexity taken by a statement or group of statements. T(n) = t(statement1) + t(statement2) + ... + t(statementN); If each statement executes a basic operation, we can say it takes constant time O(1) .


2 Answers

Unfortunately, infeasible. See Halting Problem.

like image 63
ferhat Avatar answered Sep 17 '22 05:09

ferhat


It's not possible in general. Here's one python program that can compute the complexity for some program: https://github.com/Mortal/complexity

like image 24
ReyCharles Avatar answered Sep 19 '22 05:09

ReyCharles