Python: Why is threaded function slower than non thread

后端 未结 2 1904
别跟我提以往
别跟我提以往 2020-12-11 19:09

Hello I\'m trying to calculate the first 10000 prime numbers.

I\'m doing this first non threaded and then splitting the calculation in 1 to 5000 and 5001 to 10000. I

2条回答
  •  青春惊慌失措
    2020-12-11 19:43

    You can use the multiprocessing module, which gives results like below:

    ('Non threaded Duration: ', 0.016599999999999997, 'seconds')
    ('Threaded Duration: ', 0.007172000000000005, 'seconds')
    

    ...after making just these changes to your code (changing 'Thread' to 'Process'):

    import math
    #from threading import Thread
    from multiprocessing import Process
    
    def nonThreaded():
        primeNtoM(1,10000)
    
    
    def threaded():
        #t1 = Thread(target=primeNtoM, args=(1,5000))
        #t2 = Thread(target=primeNtoM, args=(5001,10000))
        t1 = Process(target=primeNtoM, args=(1,5000))
        t2 = Process(target=primeNtoM, args=(5001,10000))
        t1.start()
        t2.start()
        t1.join()
        t2.join()
    

    By spawning actual OS processes instead of using in-process threading, you eliminate the GIL issues discussed in @Luis Masuelli's answer.

    multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows.

提交回复
热议问题