Why count () method is faster than python for loop

Here are 2 functions that do exactly the same, but does anyone know why the one that uses the method count()is much faster than the other? (I mean, how does it work? How is it built?)

If possible, I need a more understandable answer than found here: The algorithm used to implement the Python str.count function or what in the source code: https://hg.python.org/cpython/file/tip/Objects/stringlib/fastsearch .h

def scoring1(seq):
    score = 0
    for i in range(len(seq)):
       if seq[i] == '0':
           score += 1      
    return score

def scoring2(seq):
    score = 0
    score = seq.count('0') 
    return score

seq = 'AATTGGCCGGGGAG0CTTC0CTCC000TTTCCCCGGAAA'
# takes 1min15 when applied to 100 sequences larger than 100 000 characters
score1  = scoring1(seq)
# takes 10 sec when applied to 100 sequences larger than 100 000 characters
score2  = scoring2(seq)

Thank you very much for your response

+4
source share
2 answers

@CodeMonkey , , , 20% :

import time, random

def scoring1(seq):
    score=0
    for i in range(len(seq)):
       if seq[i]=='0':
           score+=1      
    return score

def scoring2(seq):
    score=0
    for x in seq:
       score += (x =='0')    
    return score

def scoring3(seq):
    score = 0
    score = seq.count('0') 
    return score

def test(n):
    seq = ''.join(random.choice(['0','1']) for i in range(n))
    functions = [scoring1,scoring2,scoring3]
    for i,f in enumerate(functions):
        start = time.clock()
        s = f(seq)
        elapsed = time.clock() - start
        print('scoring' + str(i+1) + ': ' + str(s) + ' computed in ' + str(elapsed) + ' seconds')

test(10**7)       

:

scoring1: 5000742 computed in 0.9651326495293333 seconds
scoring2: 5000742 computed in 0.7998054195159483 seconds
scoring3: 5000742 computed in 0.03732172598339578 seconds

count().

: , .

+3

. for .

+4

Source: https://habr.com/ru/post/1662958/


All Articles