Skip to content

Instantly share code, notes, and snippets.

@huseyinyilmaz
Created April 8, 2011 13:08
Show Gist options
  • Save huseyinyilmaz/909790 to your computer and use it in GitHub Desktop.
Save huseyinyilmaz/909790 to your computer and use it in GitHub Desktop.
experimental implementation to native len method (do not use it.)
#!/usr/bin/python3
from functools import reduce
from operator import add
from datetime import datetime
def countIter2(i):
try:
i.__next__()
except StopIteration:
return 0
return 1 + countIter2(i)
def len2(l):
return countIter2(l.__iter__())
def len3(l):
return reduce(add, [1 for i in l])
if __name__ == '__main__':
l = list(range(900))
start = datetime.now()
count = len(l)
end = datetime.now()
print('native(%d)'%count,str(end-start))
start = datetime.now()
count = len2(l)
end = datetime.now()
print('len2(%d) '%count,str(end-start))
start = datetime.now()
count = len3(l)
end = datetime.now()
print('len3(%d) '%count,str(end-start))
###########
# RESULTS #
###########
# native(900) 0:00:00.000041
# len2(900) 0:00:00.001202
# len3(900) 0:00:00.000176
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment