From 116c042f348667e8209d04aa9f9dcb7e155be245 Mon Sep 17 00:00:00 2001 From: jai-dewani Date: Fri, 22 May 2020 18:44:34 +0530 Subject: [PATCH] now saving the memory consumed in object's instance too --- funmark/{main.py => Benchmark.py} | 16 ++++--- funmark/__init__.py | 49 ++------------------ funmark/__pycache__/__init__.cpython-38.pyc | Bin 1714 -> 0 bytes 3 files changed, 13 insertions(+), 52 deletions(-) rename funmark/{main.py => Benchmark.py} (81%) delete mode 100644 funmark/__pycache__/__init__.cpython-38.pyc diff --git a/funmark/main.py b/funmark/Benchmark.py similarity index 81% rename from funmark/main.py rename to funmark/Benchmark.py index 6368bfa..3dd93da 100644 --- a/funmark/main.py +++ b/funmark/Benchmark.py @@ -1,7 +1,7 @@ import time as t import numpy as np import matplotlib.pyplot as plt - +import tracemalloc class Benchmark: @@ -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) @@ -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() \ No newline at end of file diff --git a/funmark/__init__.py b/funmark/__init__.py index 6368bfa..3291783 100644 --- a/funmark/__init__.py +++ b/funmark/__init__.py @@ -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 \ No newline at end of file diff --git a/funmark/__pycache__/__init__.cpython-38.pyc b/funmark/__pycache__/__init__.cpython-38.pyc deleted file mode 100644 index 7eddf94056674ef7cba979c75da8e8bc2c28ea1c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1714 zcmZ`(&5qkP5GKVR$zE@=N!wtPZd(WFPjuM_D2gIz)9lTMCcOyT2wG9L<>)VzoW_QH z%J#TVP!#YU^B#Q)UV7*$uh3IxDBHV9TMGO*BuC`TH#7Wcf4@(l{q@Bk&;ArJ;^cZ` zFnI{QJ_b-k(ToJtj!wYr$a4`$N=}G!6gwx1y>>`Z{Z&@iV?3j36qD=>&F0z zJuh$+Rn9qqGnwKra&<#V<(|_(C~x!i5;>Ntul8WoRX5c@?Steg0e2mk-gik6PxDAW zk13qHZ74g?zlUBQ0Zd3s!VbX^kR6NQ%oFm4j(l!-ljez$d6JiUVR)65jTwyRMckxi zF^clUr-s)_Hhx2N4^I61s~^5TJxQDC{OojR#3}rM#+-&>TBJ=F9#;$FZ6_XDgFvXX z-}3hE&*QC9jCOAul7{^Rdi@BXr8BgiHqM$|k`-%d>%69vZ05Y>EjuJD(Qrjk_csVo zD*)N=0`*#?jgOWGPp8RH&kJ;VI4<>YDThs2J|8YW9M(x(7OIB%FfQ{dOPa(N=tTi6 zgrRo94nrHwaTJwKi^i84{R%h|W_YA0FO8_1NH>Nhg$mrwRhGF9Far&P^&2462!wjn zbD+a_NSFIp+veg<>MVLK%#myJRwKrAR?`!Mdu%8m65OPwvmf&6iwP>38eY7{KfYyDf%8L>W>kyjx_qO zKLIeT%DNnQpQawx&WVnyDgIF(01!H$4`|NMtj4;Nx zWgt-n6`-68xe_gZM1DS6N#Jw4FLA#WxKGK8qgKr919M^Xf6b9A%m-hL9z){(pM&~S zxOKTVhdj|kold5WPXm^fu`l#zux0DODg`o2CYVgfi|_s?Z#Q8p;7yWOMwHDo(eJb7 zVy#6w`4SXs`#eJtc