0

I have a program like:

import time
import concurrent.futures

def foo(bar):
    my_sum = 0
    for i in range(-10000000, 10000001 + int(bar)):
        my_sum += i

    return my_sum

with concurrent.futures.ThreadPoolExecutor() as executor:
    for j in range(1,14):
        thread_arr = []
        futures = []
        t0 = time.time()
        for i in range(0, j):
            futures.append(executor.submit(foo, i))

        for future in futures:
            future.result()
        t1 = time.time() - t0
        print(f'j: {j}, t/j: {int(1000*t1/j)}; t: {int(1000* t1)} ms')

Time grows near linearly with the number of processes

j: 1, t/j: 750; t: 750 ms
j: 2, t/j: 734; t: 1469 ms
j: 3, t/j: 729; t: 2187 ms
j: 4, t/j: 730; t: 2923 ms
j: 5, t/j: 741; t: 3707 ms
j: 6, t/j: 765; t: 4595 ms
j: 7, t/j: 773; t: 5416 ms
j: 8, t/j: 751; t: 6009 ms
j: 9, t/j: 749; t: 6745 ms
j: 10, t/j: 749; t: 7493 ms
j: 11, t/j: 743; t: 8182 ms
j: 12, t/j: 740; t: 8891 ms
j: 13, t/j: 736; t: 9568 ms

The execution time is growing linearly as if the execution is conservative, not parallel. How can I execute this in parallel?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stepan
  • 1,391
  • 18
  • 40
  • Threads are not processes. Also, assuming you are using "C" python, `foo` in this case is CPU bound and therefore cannot be run in true parallel using threads because of the GIL. – Axe319 Nov 09 '20 at 10:41
  • For more details, see https://stackoverflow.com/q/1294382/12479639 – Axe319 Nov 09 '20 at 10:45

0 Answers0