Python library to measure the complexity of your algorithms using the following asymptotic notations:
- Big O - pycomplexity.BigO
- Big Ω (omega) - pycomplexity.BigOmega
- Big Θ (theta) - pycomplexity.BigTheta
To install using pip
:
$ pip install pycomplexity
Simple examples of using Big O notation. Here are the most popular and common examples.
from pycomplexity import BigO
big_o = BigO(full_report=True) # change to False if you need brief info
@big_o.complexity
def my_lovely_function():
a = 1
b = 2
c = a + b
return c
Console returns output
:
Function name: my_lovely_function
Function attributes: no attributes
-------------- BIG O --------------
Сomplexity of algorithm: O(1)
Memory of algorithm: O(1)
Or when you set full_report=False
the console output
:
Сomplexity of algorithm: O(1)
Memory of algorithm: O(1)
Using arr
as variable:
@big_o.complexity
def your_func():
count = 0
arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # fixed length
for el in arr:
count += el
return count
Console output
:
Сomplexity of algorithm: O(1)
Memory of algorithm: O(1)
Using arr
as attribute:
@big_o.complexity
def your_func(arr: List[int]) -> int:
# not fixed length
count = 0
for el in arr:
count += el
return count
Console output
:
Сomplexity of algorithm: O(N)
Memory of algorithm: O(1)
Copyright (c) 2023-present, Hagai