Created
October 28, 2018 08:11
-
-
Save return0927/1bada3523e5dab740787752fd2b39dc1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import sample | |
import multiprocessing as mp | |
import time | |
MAX_VAL = 100 | |
makeNumbers = lambda count: sample(range(1, MAX_VAL + 1), count) | |
class Case: | |
def __init__(self, **kwargs): | |
self.num_count = kwargs.get("num_count", 5) | |
self.interval = kwargs.get("interval", 10) | |
self.numbers = [] | |
def _set_numbers(self): | |
Numbers = makeNumbers(self.num_count) | |
Numbers.sort() | |
return Numbers | |
def _run(self, now_num = 0, cycle=0, stack=[]): | |
if not stack: | |
stack = self._set_numbers() | |
cycle += 1 | |
if stack[-2] <= now_num: | |
if stack[-1] > now_num: | |
return cycle, True | |
else: | |
return cycle, False | |
return self._run(now_num+self.interval, cycle, stack) | |
def _runner(self): | |
return self._run() | |
TestCount = 1000000 | |
LoopCount = 10 | |
# Interval = 20 | |
if __name__ == "__main__": | |
f = open("result.txt", "a") | |
f.write("TestCount, {}\nLoopCount, {}\n\n".format(TestCount, LoopCount)) | |
f.write("Interval, {}\n".format(", ".join(["Test"+str(n+1)+", Estimated" for n in range(LoopCount)]))) | |
for Interval in range(1, 101): | |
Tester = Case(num_count=5, interval=Interval) | |
f.write("{}, ".format(Interval)) | |
totalTime = 0 | |
totalTrues = 0 | |
totalCount = LoopCount * TestCount | |
pool = mp.Pool(8) | |
for n in range(LoopCount): | |
start = time.time() | |
Results = pool.map(Tester._runner, [None for n in range(TestCount)]) | |
end = time.time() | |
# print(Results) | |
Trues = [x[1] for x in Results].count(True) | |
f.write(f"{Trues}, {end - start}, ") | |
totalTime += end - start | |
totalTrues += Trues | |
f.write(f"{totalTrues}, {totalCount}, {totalTime}\n") | |
print("{:,} Tests with Interval {}, {:,} of {:,} : {:}% (Runtime: {:.5f}s)".format(TestCount, Interval, totalTrues, totalCount, totalTrues / totalCount * 100, totalTime)) | |
# print(Performance) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment