get_substrings

Format: Python
 ( View Raw)
Date: Sun, 22 Sep 2024 at 09:24:46

import timeit

print(timeit.timeit(setup='''
from string import ascii_letters
def get_substrings_horizontal(string):
    strlen = len(string)
    o = 0 # number of iterations done
    inc = 1 # increment the offset at each iteration
 
    results = []
    while o <= (strlen*(strlen+1)/2)-1:
        s = 0 # from this string's idx
        e = 0 # to this string's idx
        for x in range((strlen+1)-inc):
            results.append([string[s:e+inc], s, e+inc, inc, o])
            s += 1
            e += 1
            o += 1
        inc += 1
''',
stmt='get_substrings_horizontal(ascii_letters)',
number=10000))



print(timeit.timeit(setup='''
from string import ascii_letters
def get_substrings(string):
    strlen = len(string)
    o = 0
    results = []
    for s in range(strlen):
        for e in range(s+1, strlen+1):
            o +=1
            results.append((string[s:e], s, e, o))
''',
stmt='get_substrings(ascii_letters)',
number=10000))