0

Problems understanding how threads work.

I'm learning threading in Python. I decided to calculate the sum of the array using threads, that is, calculate the first half in one thread, and the second in another thread and calculate the time. Here is my code:

import threading
import math
import time

def part_summa0(arr, i, j):
    global summ0
    for e in range(i, j):
        summ0 += arr[e]

def part_summa1(arr, i, j):
    global summ1
    for e in range(i, j):
        summ1 += arr[e]

def threads(arr):
    threads = []
    threads.append(threading.Thread(target=part_summa0, args=(arr, 0, len(arr)//2)))
    threads.append(threading.Thread(target=part_summa1, args=(arr, len(arr)//2, len(arr))))
    for thread in threads:
        thread.start()
    for thread in threads:
        thread.join()    


l = list(range(1,100000000))
summ0 = 0
summ1 = 0
start = time.time()
part_summa0(l, 0, len(l))
print(summ0)
print(time.time()-start)

summ0 = 0
summ1 = 0
start = time.time()
threads(l)
print(summ0+summ1)
print(time.time()-start)

Here is the output:

4999999950000000
8.55412483215332
4999999950000000
15.346555471420288

Can someone explain to me why threads are slow and how it works?

  • Try doing something. Store the result of each thread in two different variables and only in the end add the two variables together. Run again and update the question to see where this is going. – ARK1375 Mar 05 '23 at 11:27
  • @ARK1375 I did what you said, still slow, what is the reason? – Kirill Stepankov Mar 05 '23 at 11:36
  • Read about GIL. In short, __threading__ is just a method in python to bring async programming to the language. It is mostly used to lower the cost of i/o interaction not to speed up the program. Your code will still run on one CPU core anyway. To speed up the code and use more than one CPU core, you need to use __Multiprocessing__. – ARK1375 Mar 05 '23 at 11:42
  • @ARK1375 But i don't understand why this code works correctly: `import threading` `import time` `def add(a,b):` `print(a+b)` `time.sleep(5)` `t = threading.Thread(target=add, args=(1,4))` `t.start()` `print('hello')` – Kirill Stepankov Mar 05 '23 at 11:46
  • Re the behaviour of `time.sleep`, see: https://stackoverflow.com/questions/61809897/why-does-time-sleep-not-get-affected-by-the-gil – slothrop Mar 05 '23 at 11:54
  • So when you execute your code, there is at least one thread running on the CPU. That is the main thread in your code. Now you add another thread and the CPU has to pay attention to both of theses. Runs one line of thread 1, one line of thread 2 and so on. When you use `sleep` function, you tell the CPU to ignore that thread for `s` seconds. But when you are adding numbers in both threads, the CPU is constantly executing code. That is why in the `sleep` case the code is faster. The CPU doesn't have any code to run in your second thread. – ARK1375 Mar 05 '23 at 11:55
  • @ARK1375 thank you very much, now I have become much clearer the difference in these two programs and how everything works! – Kirill Stepankov Mar 05 '23 at 11:57
  • @KirillStepankov Any time :) – ARK1375 Mar 05 '23 at 11:58

0 Answers0