Browse Source

Verbesserung der Parameterübergabe und Berechnung aus Zahlenliste.

develop
Olli Graf 2 years ago
parent
commit
b580f7fd5b
  1. 24
      teil12/pool_squares.py

24
teil12/pool_squares.py

@ -14,38 +14,48 @@ def print_verbose(s):
if __verbose__: if __verbose__:
print(s) print(s)
def calc_square(x, *args,**kwargs): def calc_sum_square(zahlen, *args,**kwargs):
start = kwargs['start'] start = kwargs['start']
ende = kwargs['end'] ende = kwargs['end']
threadnr = kwargs['threadnr'] threadnr = kwargs['threadnr']
summe = 0 summe = 0
print_verbose(f'args={args}') # print_verbose(f'x={x}')
time.sleep(1.0) # time.sleep(1.0)
print_verbose(f'kwargs={kwargs}') # print_verbose(f'kwargs={kwargs}')
print_verbose(f'von {start} bis {ende}') print_verbose(f'von {start} bis {ende}')
for n in range(start,ende): for n in range(start,ende):
print_verbose(f'Berechnung:{threadnr}: {n}, {n*n}') i = zahlen[n]
summe = summe + n*n print_verbose(f'Berechnung:{threadnr}: {i}, {i*i}')
summe = summe + i*i
return summe return summe
def smap(f, *arg): def smap(f, *arg):
args, kwargs = arg args, kwargs = arg
# print(f'smap() args= {args}')
time.sleep(1.0)
# print(f'smap() kwargs={kwargs}')
time.sleep(1.0)
return f(list(args), **kwargs) return f(list(args), **kwargs)
if __name__ == '__main__': if __name__ == '__main__':
start = timer()
zahlen = [i for i in range(10000000)] zahlen = [i for i in range(10000000)]
ende = timer()
print(f'Zahlenliste erzeugt in {ende-start}s')
split = int(len(zahlen) / 3) split = int(len(zahlen) / 3)
print(f'split={split}') print(f'split={split}')
time.sleep(1.0) time.sleep(1.0)
start = timer() start = timer()
with Pool(processes=3) as pool: with Pool(processes=3) as pool:
result = pool.starmap(smap, [(calc_square, zahlen, {'threadnr':1,'start':0, 'end':split}), (calc_square, zahlen, {'threadnr':2,'start':split+1,'end':split*2}), (calc_square, zahlen,{'threadnr':3,'start':(split*2)+1,'end':split*3})]) result = pool.starmap(smap, [(calc_sum_square, zahlen, {'threadnr':1,'start':0, 'end':split}), (calc_sum_square, zahlen, {'threadnr':2,'start':split+1,'end':split*2}), (calc_sum_square, zahlen,{'threadnr':3,'start':(split*2)+1,'end':split*3})])
print(f'Ergebnis: {result}') print(f'Ergebnis: {result}')
ende = timer() ende = timer()
print(f'Ausführzeit: {ende-start}s') print(f'Ausführzeit: {ende-start}s')

Loading…
Cancel
Save