Add files via upload

This commit is contained in:
Mateusz779 2021-08-27 12:48:25 +02:00 committed by GitHub
parent 5993fb6803
commit 1547d90f30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 33 deletions

View File

@ -1,14 +1,26 @@
# #!/usr/bin/env python3
# @Mateusz779 # -*- coding: utf-8 -*-
#
"""
@author: Mateusz779
"""
from decimal import Decimal from decimal import Decimal
from decimal import getcontext from decimal import getcontext
import time import time
from multiprocessing.dummy import Pool as Pool #pip3 install multiprocess from multiprocessing.dummy import Pool as Pool
from tqdm import tqdm #pip3 install tqdm from tqdm import tqdm
import argparse
threads=4 #threads number parser = argparse.ArgumentParser(description='The script computes the number pi.')
count = 10000 #number of numbers after the point parser.add_argument('-t', '--threads', help="Number of threads deffault 4", required=False,type=int, default=4)
parser.add_argument('-c', '--count', help="Number of numbers after the point deffault 10000", required=False,type=int, default=10000)
parser.add_argument('-f', '--filename', help="File to save result", required=False)
args = parser.parse_args()
threads=args.threads
count = args.count #number of numbers after the point
thread_list = [] thread_list = []
count=count+1 count=count+1
@ -23,19 +35,14 @@ def pi(start):
start_table = [] start_table = []
if count >500: if count >500:
th=int(threads*(count/1000)) tasks=int(threads*(count/1000))
else: else:
th=1 tasks=1
while count%th!=0:
if count>th:
th=th+1
else:
th=th-1
one_count=int(count/th) one_count=int(count/tasks)
for i in range(th): def main():
start_table.append(int((one_count*i))) start_table = [int((one_count * i)) for i in range(tasks)]
start = time.time() start = time.time()
with Pool(processes=threads) as pool: with Pool(processes=threads) as pool:
@ -54,3 +61,17 @@ for i in sort:
print(Decimal(result)) print(Decimal(result))
print("\n\n") print("\n\n")
print("Elapsed time: ",end - start,"s\n") print("Elapsed time: ",end - start,"s\n")
if args.filename!=None:
f = open(args.filename, "w")
f.write(str(Decimal(result)))
f.close()
if __name__ == "__main__":
main()

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
# Automatically generated by https://github.com/damnever/pigar.
# pi/BBP multithreading.py: 13
tqdm == 4.62.2