Last active
June 21, 2022 06:30
-
-
Save tropicbliss/8622ce13bfd07f0fe777db321db64607 to your computer and use it in GitHub Desktop.
Modelling algorithm complexity using a chart in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pandas import DataFrame | |
import altair as alt | |
import timeit | |
def gcd_brute_force(a, b): | |
pass | |
def gcd_dijkstra(a, b): | |
pass | |
def test(a, b): | |
repeats = 100000 | |
print("a=%d, b=%d" % (a, b)) | |
# timeit repeats the code fragment by *repeats* times | |
t1 = timeit.timeit("gcd_brute_force(%d,%d)" % ( | |
a, b), setup="from __main__ import gcd_brute_force", number=repeats) | |
t2 = timeit.timeit("gcd_dijkstra(%d,%d)" % ( | |
a, b), setup="from __main__ import gcd_dijkstra", number=repeats) | |
print(" - Brute Force: ", t1) | |
print(" - Dijkstra's : ", t2) | |
return (t1, t2) | |
list_a = [] | |
time_a = [] | |
time_b = [] | |
list_a = [81, 330, 1989] | |
list_b = [36, 231, 867] | |
for a, b in zip(list_a, list_b): | |
t_bf, t_d = test(a, b) | |
time_a.append(t_bf) | |
time_b.append(t_d) | |
data_a = DataFrame([list_a, time_a]).transpose() | |
data_b = DataFrame([list_a, time_b]).transpose() | |
data_a.columns = ['a', 'time_a'] | |
data_b.columns = ['a', 'time_b'] | |
chart_a1 = alt.Chart(data_a).mark_line( | |
point=True, color='red').encode(x='a', y='time_a') | |
chart_a2 = alt.Chart(data_a).mark_circle( | |
color='red', size=60).encode(x='a', y='time_a') | |
chart_b1 = alt.Chart(data_b).mark_line( | |
point=True, color='blue').encode(x='a', y='time_b') | |
chart_b2 = alt.Chart(data_b).mark_circle( | |
color='blue', size=60).encode(x='a', y='time_b') | |
chart = chart_a1 + chart_a2 + chart_b1 + chart_b2 | |
chart |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment