import timeit
s = """
from collections import Iterable
def flatten(l):
   for e in l:
       if isinstance(e, Iterable):
           yield from flatten(e)
       else:
           yield e 
def build_lots(N):
    l = [-1]
    for i in range(N):
        l = [i, [l], i]
    return l
def build_some(N):
    l = [-1]
    for i in range(N):
        l = [i]+l+[i] if i % 2 else [i,[l],i]
    return l
def build_none(N):
    return range(2*N+1)
"""

def get_time(size, which_build, n=100):
    setup = s + 'l = build_{}({});'.format(which_build, size)
ans = timeit.Timer(setup=setup, stmt='z=list(flatten(l))').timeit(n)
return ans
print(('length', 'none', 'some', 'lots'))
for w in range(0, 500, 50):
    print((2 * w + 1, get_time(w, 'none'), get_time(w, 'some'), get_time(w,
        'lots')))
Выход Python 3.3 из