Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

While I'm profiling my Python application, I've found that len() is by all accounts an extravagant one when utilizing sets. See the underneath code: 

import cProfile

def lenA(s):

    for i in range(1000000):

        len(s);

def lenB(s):

    for i in range(1000000):

        s.__len__();

def main():

    s = set();

    lenA(s);

    lenB(s);

if __name__ == "__main__":

    cProfile.run("main()","stats");

As indicated by the profiler's details underneath, lenA() is by all accounts 14 times more slow than lenB()

ncalls  tottime  percall  cumtime  percall  filename:lineno(function)

      1    1.986    1.986    3.830    3.830  .../lentest.py:5(lenA)

1000000    1.845    0.000    1.845    0.000  {built-in method len}

      1    0.273    0.273    0.273    0.273  .../lentest.py:9(lenB)

Am I missing anything? Right now I use __len__() rather than len(), yet the code looks filthy :(

1 Answer

0 votes
by (26.4k points)

Clearly, len has some overhead and since it does a function call and makes an interpretation of AttributeError to TypeError. Additionally, set.__len__ is a particularly basic operation that it will undoubtedly be exceptionally quick in contrast with pretty much anything, yet I actually don't discover anything like the 14x distinction when utilizing timeit

In [1]: s = set()

In [2]: %timeit s.__len__()

1000000 loops, best of 3: 197 ns per loop

In [3]: %timeit len(s)

10000000 loops, best of 3: 130 ns per loop

You should in every case simply call len, not __len__. In the event that the call to len is the bottleneck in your program, you should reexamine its design, for example, cache measures someplace or ascertain them without calling len.

Are you pretty much interested to learn python in detail? Come and join the python training course to gain more knowledge.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 22, 2019 in Python by Eresh Kumar (45.3k points)
0 votes
1 answer
asked Oct 14, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...