We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Just a note, in my implementation I discovered that checking string equality is almost an order of magnitude faster than if int(). See below, on Python 2 (though I did my submission on Python 3 and it passed with string equality and not with int())
python -mtimeit -s'xs="10110110101010101"' 'popcount = sum([1 for x in xs if int(x)])'
100000 loops, best of 3: 15.5 usec per loop
vs.
python -mtimeit -s'xs="10110110101010101"' 'popcount = sum([1 for x in xs if x =='1'])'
100000 loops, best of 3: 1.99 usec per loop
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
ACM ICPC Team
You are viewing a single comment's thread. Return to all comments →
Just a note, in my implementation I discovered that checking string equality is almost an order of magnitude faster than
if int()
. See below, on Python 2 (though I did my submission on Python 3 and it passed with string equality and not withint()
)python -mtimeit -s'xs="10110110101010101"' 'popcount = sum([1 for x in xs if int(x)])'
100000 loops, best of 3: 15.5 usec per loop
vs.
python -mtimeit -s'xs="10110110101010101"' 'popcount = sum([1 for x in xs if x =='1'])' 100000 loops, best of 3: 1.99 usec per loop