Skip to content

Commit

Permalink
Add seprate plotting methods for memory
Browse files Browse the repository at this point in the history
  • Loading branch information
jai-dewani committed May 22, 2020
2 parents 22b6c3d + 9571306 commit ae20139
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 111 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ __pycache__/
build/
funmark.egg-info/
*.pyc
funmark/
Binary file modified dist/funmark-0.1.1.dev2-py3-none-any.whl
Binary file not shown.
Binary file modified dist/funmark-0.1.1.dev2.tar.gz
Binary file not shown.
47 changes: 35 additions & 12 deletions funmark/Benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ def __init__(self):
self.functionName = None

def run(self, func, *argv):
"""
return:
runtime: int, the time it took to run (seconds)
memory: int, total memeory took by the function (KB)
"""
self.functionName = func.__name__
tracemalloc.start()
start = t.time()
Expand All @@ -21,17 +26,18 @@ def run(self, func, *argv):
__,peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
runtime = end - start
print(f'The runtime for {func.__name__} took {runtime} seconds to complete')
return runtime, peak/10**6
print(f'The runtime for {func.__name__} took {runtime} seconds to complete with {peak/10**3}KB memory')
return runtime, peak/10**3

def add(self, index, time=None, memory=None):
if time:
if time!=None:
self.time.append([index, time])
if memory:
if memory!=None:
self.memory.append([index, memory])

def plotGraph(self, xlabel="", ylabel="", title="", label="", show=True, plt=plt):
def plotTime(self, xlabel="", ylabel="", title="", label="", show=True, plt=plt):
time = np.array(self.time)
print('time',time)
plt.plot(time[:,0],time[:,1], label=label)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
Expand All @@ -40,11 +46,28 @@ def plotGraph(self, xlabel="", ylabel="", title="", label="", show=True, plt=plt
plt.show()
return plt

def compare(self, xlabel="", ylabel="", title="", *argv):
temp = self.plotGraph(xlabel=xlabel, ylabel=ylabel, title=title, label=self.functionName, show=False, plt=plt)
def plotMemory(self, xlabel="", ylabel="", title="", label="", show=True, plt=plt):
memory = np.array(self.memory)
print('memory',memory)
plt.plot(memory[:,0],memory[:,1], label=label)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)
if show:
plt.show()
return plt

def compareTime(self, xlabel="", ylabel="", title="", *argv):
time_plt = self.plotTime(xlabel=xlabel, ylabel=ylabel, title=title, label=self.functionName, show=False, plt=plt)
for other in argv:
other.plotTime(xlabel=xlabel, ylabel=ylabel, title=title, label=other.functionName, show=False, plt=time_plt)
time_plt.legend(loc="upper left")
time_plt.show()


def compareMemory(self, xlabel="", ylabel="", title="", *argv):
memory_plt = self.plotMemory(xlabel=xlabel, ylabel=ylabel, title=title, label=self.functionName, show=False, plt=plt)
for other in argv:
# time2 = np.array(other.time, plt=temp)
# plt.plot(time2[:,0],time2[:,1])
other.plotGraph(xlabel=xlabel, ylabel=ylabel, title=title, label=other.functionName, show=False, plt=temp)
temp.legend(loc="upper right")
temp.show()
other.plotMemory(xlabel=xlabel, ylabel=ylabel, title=title, label=other.functionName, show=False, plt=memory_plt)
memory_plt.legend(loc="upper left")
memory_plt.show()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
setuptools.setup(
name='funmark',
version='0.1.1.dev2',
scripts=['funmark/main.py'] ,
scripts=['funmark/Benchmark.py'] ,
author="Jai Kumar Dewani",
author_email="[email protected]",
description="A benchmarking tool for fuctions",
Expand Down
Empty file removed tests/__init__.py
Empty file.
5 changes: 3 additions & 2 deletions test.py → tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def mergeSort(argv):
quick = funmark.Benchmark()
tim = funmark.Benchmark()
i = 10
top = 10**5
top = 10**4
while i<top:
print(i,top)
ar = [randint(1,10**5) for i in range(i)]
Expand All @@ -90,4 +90,5 @@ def mergeSort(argv):

i = int(2*i)

merge.compare("Length", "Time", "Sort comparision", quick, tim)
merge.compareTime("Length", "Time", "Compression between Sorting Algorithms", quick, tim)
merge.compareMemory("Length", "Time", "Compression between Sorting Algorithms", quick, tim)
96 changes: 0 additions & 96 deletions tests/test2.py

This file was deleted.

0 comments on commit ae20139

Please sign in to comment.