DEV Community

joaquinperea
joaquinperea

Posted on

How can I measure Big O complexity in a Python method?

Hi everyone! Thanks for reading.
I need to solve a problem where, first of all, I add certain number of integer values to a list called "values_added". After that, I need to implement a method called "build_stats" which prepare the data to be analyzed by another methods.
I have this portion of code which I need to be build in a Big O complexity equals to O(n):

def build_stats(self):
        """
        Function that generates the necessary statistics structure to be consumed
        through the "stats" object.
        :return: Stats object.
        """
        self.values_added.sort()
        for i, elem in enumerate(self.values_added):
            if elem not in self.values_stats:
                self.values_stats[elem] = {
                    'left': i,
                    'right': len(self.values_added) - i - 1,
                    'count': 1
                }
            else:
                self.values_stats[elem]['right'] = len(self.values_added) - i - 1
                self.values_stats[elem]['count'] += 1
        return Stats(self.values_added, self.values_stats)
Enter fullscreen mode Exit fullscreen mode

I need to check if this method has a time complexity = O(n).
Thanks!

Top comments (0)