I am comparing runtimes of different ways of flattening a list of lists using the big_o module, and for following methods my function does not return the expected results, namely:
- This one:
def itertools_chain_from_iterable(arr):
return list(chain.from_iterable(arr))
returns "constant", which can't possibly be true.
- This one:
def merge_extend(self,arr):
output = []
for l in arr:
output.extend(l)
return output
returns "cubic" (shouldn't it be quadratic at most?), while...
- ..this one
def merge_w_sum(self,arr):
return sum(arr,[])
returns "linear" (I'm quite sure it should be quadratic, see proof here.
- Furthermore, the list comprehension one
def merge_bucket(self,bucket):
return [number for n in bucket for number in n]
returns "polynomial", which seems terrifying (would expect linear here as well)
Code used to calculate the complexities:
print('<function name>:', big_o.big_o(<function name>,
lambda n:[big_o.datagen.integers(9900,1,9999999) for n in range(50)],
n_measures=20)[0])
Output:
complexity of itertools_chain_from_iterable: Constant: time = 0.0013 (sec)
complexity of merge_w_sum: Linear: time = 0.46 + 6.2E-07*n (sec)
complexity of merge_extend: Cubic: time = 0.048 + -2.3E-18*n^3 (sec)
complexity of merge_bucket: Polynomial: time = 0.2 * x^-0.019 (sec)
What is it that I'm doing (or understanding) wrong? Many thanks in advance for useful tips!