This commit is contained in:
ducoterra
2018-08-14 00:20:31 -04:00
parent 47c752ff65
commit 0a652e8f4c
6 changed files with 142 additions and 7 deletions

1
.gitignore vendored
View File

@@ -1,5 +1,6 @@
venv/
*.xlsx
*.docx
*.pyc
.idea/
__pycache__/

65
addit.py Normal file
View File

@@ -0,0 +1,65 @@
from time import time
from threading import Thread
from multiprocessing import Process
def timeit(func):
"""
prints the time a function takes to run
:param func:
:return:
"""
def wrapper(*args):
t1 = time()
func(*args)
t2 = time()
print("Time to run: " + str(t2 - t1))
return wrapper
def printit(func):
"""
prints a string before and after the function
:param func:
:return: wrapper
"""
def wrapper(*args):
print("Starting " + str(args))
func(*args)
print(str(args) + " done!")
return wrapper
def threadit(func):
"""
starts a function in its own thread
:param func: the function
:return: wrapper
"""
def wrapper(*args):
t = Thread(target=func, args=args)
t.start()
return wrapper
def processit(func):
"""
starts a function in its own process
:param func: the function
:return: wrapper
"""
def wrapper(*args):
p = Process(target=func, args=args)
p.start()
return p
return wrapper
@processit
def test():
while True:
pass

View File

@@ -1,5 +1,4 @@
from timer import timing_function
from addit import timeit, printit, threadit, processit
def isprime(number, primelist):
"""
@@ -12,7 +11,10 @@ def isprime(number, primelist):
return True
@timing_function
@processit
@printit
@timeit
def findprimesto(primecap):
"""
finds numprimes count of prime numbers
@@ -29,7 +31,10 @@ def findprimesto(primecap):
return primelist
@timing_function
@processit
@printit
@timeit
def findxprimes(x):
"""
finds numprimes count of prime numbers
@@ -45,3 +50,14 @@ def findxprimes(x):
testnum += 2
return primelist
@timeit
def runner():
findxprimes(10000)
findxprimes(10000)
runner()
findprimesto(1000)
findprimesto(1000)

View File

@@ -1,4 +1,4 @@
from timer import timing_function
from addit import timeit, printit, threadit, processit
def isprime(number):
@@ -12,12 +12,14 @@ def isprime(number):
return True
@timing_function
@timeit
def findprimesto(primecap):
for i in range(0, primecap):
isprime(i)
@timing_function
@timeit
def findxprimes(x):
primelist = []
current_num = 2
@@ -29,3 +31,6 @@ def findxprimes(x):
current_num += 1
return primelist
findxprimes(1000)

39
threadfindprime.py Normal file
View File

@@ -0,0 +1,39 @@
from addit import timeit, processit
@processit
def isprime(number, primelist):
"""
checks if a number is prime by finding dividing it by every other prime
below floor(number / 2)
"""
print(primelist)
for prime in primelist:
if prime <= number // 2 and number % prime == 0:
return False
return True
@timeit
def findxprimes(x):
"""
finds numprimes count of prime numbers
"""
primelist = [2]
testnum = 3
cores = 4
usedcores = 0
while len(primelist) < x:
usedcores += 1
if isprime(testnum, primelist) and usedcores < 4:
primelist.append(testnum)
usedcores -= 1
testnum += 2
return primelist
findxprimes(5)

View File

@@ -13,3 +13,12 @@ def timing_function(prime_finder, *args):
t2 = time.time()
return result, t2 - t1
return wrapper
def printer(func, *args):
def wrapper(*args):
print("hello")
result = func(*args)
print("goodbye")
return result
return wrapper