Skip to content

.predict()

Jacob Morris edited this page Jun 8, 2019 · 1 revision

.predict()

Timer.predict() is a method that relies on the output of .best_fit_curve() and one or more arguments to predict execution time. The method requires at least two parameters: parameters and then at least one other positional or keyword argument to use as the independent variable along with the best-fit-curve.

Important: parameters must be an output from .best_fit_curve() and all positional and keyword argument values to use the independent variables must be integers

def bubble_sort(array):
    while True:
        switched = False
        for i in range(0, len(array)-1):
            if array[i] > array[i+1]:
                array[i], array[i+1] = array[i+1], array[i]
                switched = True

        if not switched:
            break

    return array


timer = Timer()
timer.time_it(bubble_sort, lambda: [randint(0, 10000) for _ in range(randint(100, 3000))], runs=20, log_arguments=True,
              call_callable_args=True)
params = timer.best_fit_curve(transformers={0: len})

print(params)
# ('Polynomial', {'a': 1.561077168232685e-07, 'b': 0.0002276152232480748, 0: 0, 'c': -0.0896439366132331})
print("bubble_sort(5000) will likely take {} s".format(timer.predict(params, 5000, time_unit=timer.S)))
# bubble_sort(5000) will likely take 4.9511251 s

timer.time_it(bubble_sort, [randint(0, 10000) for _ in range(5000)])
timer.output(split_index=1)
# bubble_sort:
#        6.44580 s  - bubble_sort                                [runs=  1, iterations=  1]  
Clone this wiki locally