Skip to content

Commit

Permalink
now saving the memory consumed in object's instance too
Browse files Browse the repository at this point in the history
  • Loading branch information
jai-dewani committed May 22, 2020
1 parent 0b7c3de commit 116c042
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 52 deletions.
16 changes: 10 additions & 6 deletions funmark/main.py → funmark/Benchmark.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time as t
import numpy as np
import matplotlib.pyplot as plt

import tracemalloc

class Benchmark:

Expand All @@ -13,17 +13,22 @@ def __init__(self):

def run(self, func, *argv):
self.functionName = func.__name__
tracemalloc.start()
start = t.time()
# print(var)
func(argv)
end = t.time()
__,peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
runtime = end - start
print(f'The runtime for {func.__name__} took {runtime} seconds to complete')
return runtime
return runtime, peak/10**6

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

def plotGraph(self, xlabel="", ylabel="", title="", label="", show=True, plt=plt):
time = np.array(self.time)
Expand All @@ -42,5 +47,4 @@ def compare(self, xlabel="", ylabel="", title="", *argv):
# 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()

temp.show()
49 changes: 3 additions & 46 deletions funmark/__init__.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,3 @@
import time as t
import numpy as np
import matplotlib.pyplot as plt


class Benchmark:

def __init__(self):
self.time = []
self.memory = []
self.plot = None
self.functionName = None

def run(self, func, *argv):
self.functionName = func.__name__
start = t.time()
# print(var)
func(argv)
end = t.time()
runtime = end - start
print(f'The runtime for {func.__name__} took {runtime} seconds to complete')
return runtime

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

def plotGraph(self, xlabel="", ylabel="", title="", label="", show=True, plt=plt):
time = np.array(self.time)
plt.plot(time[:,0],time[:,1], label=label)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)
if show:
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)
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()

from .Benchmark import Benchmark as bm
__all__ = ["Benchmark"]
Benchmark = bm
Binary file removed funmark/__pycache__/__init__.cpython-38.pyc
Binary file not shown.

0 comments on commit 116c042

Please sign in to comment.