0

I am trying to investigate threading by looking for primes in lists of random values. I expected to find that using threading made this faster but the times for threaded and un-threaded are the same. Is this because I have implemented this incorrectly? How can I demonstrate the benefits of threading using an example like this?

import time
import threading
from math import sqrt
from random import randint

def findPrimes(aList):
    for testValue in aList:
        isPrime = True
        for i in range(2,int(sqrt(testValue)+1)):
            if testValue % i == 0:
                isPrime = False
        if isPrime:
            #print(testValue)
            pass

testValues1 = []
testValues2 = []
for i in range(1000):
    testValues1.append(randint(10,100))
    testValues2.append(randint(10,100))


t = time.process_time()
findPrimes(testValues1)
findPrimes(testValues2)
print('The long way',time.process_time() - t) # runs in 0.006 to 0.007


t = time.process_time()
thread1 = threading.Thread(target=findPrimes(testValues1))
thread2 = threading.Thread(target=findPrimes(testValues2))
thread1.start()
thread2.start()
print('The threading way',time.process_time() - t) # also runs in 0.006 to 0.007
Andrew H
  • 466
  • 10
  • 22
  • Why would you expect this to go faster? That is, what is there about this problem that would benefit from threading? – Scott Hunter May 21 '18 at 13:34
  • The tests are way too short for any sort of statistical meaningfulness between the two. In addition, you need to call `thread1.join()` and `thread2.join()` before measuring the time elapsed, or you'll be measuring nothing at all, as the threads will still be running. – AKX May 21 '18 at 13:35
  • Threading will not speed up your code due to the [**dreaded GIL**](https://wiki.python.org/moin/GlobalInterpreterLock). Read more in [**this answer**](https://stackoverflow.com/a/18114882/7553525) and see [**this answer**](https://stackoverflow.com/a/44525554/7553525) for a speed-up demonstration using [`multiprocessing`](https://docs.python.org/3/library/multiprocessing.html). – zwer May 21 '18 at 13:36

2 Answers2

0

Threads in Python are only useful for IO-bound operations due to the Global Interpreter Lock (GIL) which essentially forces Python operations to occur in lockstep, never actually concurrently.

(However, threads that do IO, e.g. talk to a remote server or access files, can and usually are faster than no threads.)

See How to use threading in Python? for solutions such as using the multiprocessing module instead.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • "Concurrently" may not mean what you think it means. Python threads do run [concurrently](https://en.wikipedia.org/wiki/Concurrent_computing), but the GIL prevents them from running in _[parallel](https://en.wikipedia.org/wiki/Parallel_computing)_. – Solomon Slow May 21 '18 at 14:08
0

Threads aren't run concurrently... The concept of threading is that a particular thread is able to share cpu clock cycle with a number of other threads which can appear to mimic concurrent behaviour but they don't run parallel

PandaSurge
  • 370
  • 1
  • 7
  • 18