In [1]:
from pathlib import Path
import sys
# add .. to import path
sys.path.append(str(Path.cwd().parent))
from common import *
from matplotlib import pyplot
import pandas as pd
import seaborn as sns
%matplotlib inline
sns.set(style="whitegrid", palette="muted", color_codes=True)
In [2]:
%load_ext autoreload
%autoreload 2
In [3]:
from algorithms.sorting.simple import bubble_sort
class BubbleBench(Benchmark):
def setup(self, n):
self.xs = np.random.random(n)
def run(self):
ys_ = bubble_sort(self.xs, 0, len(self.xs) - 1)
bubble = BubbleBench()
In [177]:
ns1, ys1 = time_range(bubble, 1, 1000)
pyplot.plot(ns1, ys1)
Out[177]:
In [175]:
ys1_0 = time_dist(bubble, 1000, num=100)
sns.distplot(ys1_0, hist=True, rug=True)
pd.Series(ys1_0).describe()
Out[175]:
In [174]:
ys1_1 = time_dist(bubble, 1000, n_times=3)
sns.distplot(ys1_1, hist=True, rug=True)
pd.Series(ys1_1).describe()
Out[174]:
In [181]:
visualize_range_bench(ns1, ys1)
In [183]:
ns, ys = auto_bench(bubble, 15.0, range(1, 300, 1))
In [4]:
from algorithms.sorting.simple import quick_sort
class QSortBench(Benchmark):
def setup(self, n):
self.xs = np.random.random(n)
def run(self):
ys_ = quick_sort(self.xs, 0, len(self.xs) - 1)
qsort = QSortBench()
In [20]:
qsort.time(100000)
Out[20]:
In [5]:
ns2, ys2 = time_range(qsort, 1, 100000, num=200)
visualize_range_bench(ns2, ys2)
pyplot.title('Quick sort')
Out[5]:
In [6]:
class STDSortBench(Benchmark):
def setup(self, n):
self.xs = np.random.random(n)
def run(self):
ys_ = sorted(self.xs)
stdsort = STDSortBench()
In [7]:
ns3, ys3 = time_range(stdsort, 1, 100000, num=200)
visualize_range_bench(ns3, ys3)
pyplot.title('std sort')
Out[7]:
In [8]:
visualize_range_bench(ns3, ys3, label='std')
visualize_range_bench(ns2, ys2, label='qsort')
pyplot.legend()
Out[8]:
In [ ]: