Skip to content

Commit

Permalink
build without memory ploting functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jai-dewani committed May 22, 2020
1 parent 116c042 commit 22b6c3d
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__pycache__/
.vscode/
build/
funmark.egg-info/
*.pyc
Empty file added build/lib/funmark/__init__.py
Empty file.
51 changes: 51 additions & 0 deletions build/lib/funmark/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# import time as t
import numpy as np
import matplotlib.pyplot as plt
import tracemalloc

class Benchmark:

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

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, peak/10**6

def add(self, index, time=None, memory=None):
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)
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()

51 changes: 51 additions & 0 deletions build/scripts-3.7/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# import time as t
import numpy as np
import matplotlib.pyplot as plt
import tracemalloc

class Benchmark:

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

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, peak/10**6

def add(self, index, time=None, memory=None):
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)
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()

46 changes: 46 additions & 0 deletions build/scripts-3.8/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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()

Binary file added dist/funmark-0.1.1.dev2-py3-none-any.whl
Binary file not shown.
Binary file added dist/funmark-0.1.1.dev2.tar.gz
Binary file not shown.
42 changes: 42 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup(
name='funmark',
version='0.1.1.dev2',
scripts=['funmark/main.py'] ,
author="Jai Kumar Dewani",
author_email="[email protected]",
description="A benchmarking tool for fuctions",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/jai-dewani/fun-mark",
packages=setuptools.find_packages(),
classifiers=[
"Development Status :: 2 - Pre-Alpha",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.3",
"Programming Language :: Python :: 3.2",
"Programming Language :: Python :: 3.1",
"Programming Language :: Python :: 3.0",
],
install_requires=[
"numpy", "matplotlib"
],
entry_points={
'console_scripts': ['funmark=funmar:main'],
}
)

'''
Run these to deploy
python setup.py sdist bdist_wheel
twine upload --repository testpypi dist/*
'''
34 changes: 22 additions & 12 deletions tests/test.py → test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from funmark import Benchmark
import funmark
from random import randint

def s1(argv):

def timSort(argv):
ar = argv[0]
ar.sort()
return ar
Expand Down Expand Up @@ -29,7 +30,7 @@ def quickSort(argv):
quickSort([arr, pi+1, high])


def merge(arr, l, m, r):
def mergeList(arr, l, m, r):
n1 = m - l + 1
n2 = r- m
L = [0] * (n1)
Expand Down Expand Up @@ -59,25 +60,34 @@ def merge(arr, l, m, r):
k += 1

def mergeSort(argv):
# print(argv)
arr = argv[0]
l = argv[1]
r = argv[2]
if l < r:
m = (l+(r-1))//2
mergeSort([arr, l, m])
mergeSort([arr, m+1, r])
merge(arr, l, m, r)

test1 = Benchmark()
test2 = Benchmark()
mergeList(arr, l, m, r)

merge = funmark.Benchmark()
quick = funmark.Benchmark()
tim = funmark.Benchmark()
i = 10
top = 10**5
while i<top:
print(i,top)
ar = [randint(1,10**5) for i in range(i)]
time = test1.run(quickSort, ar, 0, len(ar)-1)
test1.add(len(ar),time)
time = test2.run(mergeSort, ar, 0, len(ar)-1)
test2.add(len(ar),time)

time, memory = merge.run(quickSort, ar, 0, len(ar)-1)
merge.add(len(ar), time, memory)

time, memory= quick.run(mergeSort, ar, 0, len(ar)-1)
quick.add(len(ar), time, memory)

time, memory = tim.run(timSort, ar)
tim.add(len(ar), time, memory)

i = int(2*i)
test1.compare("Length", "Time", "Sort comparision", test2)

merge.compare("Length", "Time", "Sort comparision", quick, tim)
Empty file added tests/__init__.py
Empty file.
96 changes: 96 additions & 0 deletions tests/test2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# import random
# import time

# class MyTimer():

# def __init__(self):
# self.start = time.time()

# def __enter__(self):
# return self

# def __exit__(self, exc_type, exc_val, exc_tb):
# end = time.time()
# runtime = end - self.start
# msg = 'The function took {time} seconds to complete'
# print(msg.format(time=runtime))


# def long_runner():
# for x in range(5):
# sleep_time = random.choice(range(1,5))
# time.sleep(sleep_time)

# def sort(ar):
# ar.sort()
# return ar

# if __name__ == '__main__':
# for i in range(2,7):
# with MyTimer():
# ar = [random.randint(1,10**3) for i in range(10**i)]
# sort(ar)

# ---------------------------------------------------------------


# import random
# import time

# def timerfunc(func):
# """
# A timer decorator
# """
# def function_timer(*args, **kwargs):
# """
# A nested function for timing other functions
# """
# start = time.time()
# value = func(*args, **kwargs)
# end = time.time()
# runtime = end - start
# msg = "The runtime for {func} took {time} seconds to complete"
# print(msg.format(func=func.__name__,
# time=runtime))
# return value
# return function_timer


# @timerfunc
# def long_runner():
# for x in range(5):
# sleep_time = random.choice(range(1,5))
# time.sleep(sleep_time)

# @timerfunc
# def sort(ar):
# ar.sort()
# return ar

# if __name__ == '__main__':
# for i in range(2,7):
# ar = [random.randint(1,10**3) for i in range(10**i)]
# sort(ar)

# ----------------------------------------------------------------


# import matplotlib.pyplot as plt
# ar1 = [1,2,3,4]
# ar2 = [25,7,0,2]
# ar3 = [16,74,36,22]
# ar4 = [1,2,3,4]

# def test1():
# plt.plot(ar1,ar2)
# return plt

# def test2(plt):
# plt.plot(ar1,ar3)
# # plt.show()
# return plt

# temp = test1()
# temp = test2(temp)
# temp.plot(ar1,ar4)
# temp.show()

0 comments on commit 22b6c3d

Please sign in to comment.