Skip to content

NikolaySimakov/pycomplexity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pycomplexity

PyPI version License Coverage Status Code style: black

Python library to measure the complexity of your algorithms using the following asymptotic notations:

Installation

To install using pip:

$ pip install pycomplexity

Get started

Simple examples of using Big O notation. Here are the most popular and common examples.

Basic example

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)

Attributes vs Variables

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)

License

MIT

Copyright (c) 2023-present, Hagai