init
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,5 +1,6 @@
|
|||||||
venv/
|
venv/
|
||||||
*.xlsx
|
*.xlsx
|
||||||
*.docx
|
*.docx
|
||||||
|
*.pyc
|
||||||
.idea/
|
.idea/
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
|||||||
65
addit.py
Normal file
65
addit.py
Normal 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
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
from timer import timing_function
|
from addit import timeit, printit, threadit, processit
|
||||||
|
|
||||||
|
|
||||||
def isprime(number, primelist):
|
def isprime(number, primelist):
|
||||||
"""
|
"""
|
||||||
@@ -12,7 +11,10 @@ def isprime(number, primelist):
|
|||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@timing_function
|
|
||||||
|
@processit
|
||||||
|
@printit
|
||||||
|
@timeit
|
||||||
def findprimesto(primecap):
|
def findprimesto(primecap):
|
||||||
"""
|
"""
|
||||||
finds numprimes count of prime numbers
|
finds numprimes count of prime numbers
|
||||||
@@ -29,7 +31,10 @@ def findprimesto(primecap):
|
|||||||
|
|
||||||
return primelist
|
return primelist
|
||||||
|
|
||||||
@timing_function
|
|
||||||
|
@processit
|
||||||
|
@printit
|
||||||
|
@timeit
|
||||||
def findxprimes(x):
|
def findxprimes(x):
|
||||||
"""
|
"""
|
||||||
finds numprimes count of prime numbers
|
finds numprimes count of prime numbers
|
||||||
@@ -45,3 +50,14 @@ def findxprimes(x):
|
|||||||
testnum += 2
|
testnum += 2
|
||||||
|
|
||||||
return primelist
|
return primelist
|
||||||
|
|
||||||
|
|
||||||
|
@timeit
|
||||||
|
def runner():
|
||||||
|
findxprimes(10000)
|
||||||
|
findxprimes(10000)
|
||||||
|
|
||||||
|
|
||||||
|
runner()
|
||||||
|
findprimesto(1000)
|
||||||
|
findprimesto(1000)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
from timer import timing_function
|
from addit import timeit, printit, threadit, processit
|
||||||
|
|
||||||
|
|
||||||
def isprime(number):
|
def isprime(number):
|
||||||
@@ -12,12 +12,14 @@ def isprime(number):
|
|||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@timing_function
|
|
||||||
|
@timeit
|
||||||
def findprimesto(primecap):
|
def findprimesto(primecap):
|
||||||
for i in range(0, primecap):
|
for i in range(0, primecap):
|
||||||
isprime(i)
|
isprime(i)
|
||||||
|
|
||||||
@timing_function
|
|
||||||
|
@timeit
|
||||||
def findxprimes(x):
|
def findxprimes(x):
|
||||||
primelist = []
|
primelist = []
|
||||||
current_num = 2
|
current_num = 2
|
||||||
@@ -29,3 +31,6 @@ def findxprimes(x):
|
|||||||
current_num += 1
|
current_num += 1
|
||||||
|
|
||||||
return primelist
|
return primelist
|
||||||
|
|
||||||
|
|
||||||
|
findxprimes(1000)
|
||||||
|
|||||||
39
threadfindprime.py
Normal file
39
threadfindprime.py
Normal 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)
|
||||||
9
timer.py
9
timer.py
@@ -13,3 +13,12 @@ def timing_function(prime_finder, *args):
|
|||||||
t2 = time.time()
|
t2 = time.time()
|
||||||
return result, t2 - t1
|
return result, t2 - t1
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
def printer(func, *args):
|
||||||
|
def wrapper(*args):
|
||||||
|
print("hello")
|
||||||
|
result = func(*args)
|
||||||
|
print("goodbye")
|
||||||
|
return result
|
||||||
|
return wrapper
|
||||||
Reference in New Issue
Block a user