CPU 密集型任务中的多线程和单线程性能问题 [英] Multi-threading and Single-threading performance issues in CPU-bound task

查看:77
本文介绍了CPU 密集型任务中的多线程和单线程性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下两个单线程和多线程脚本在我输入一个大数字(如 555550000

The two following single-threading and multi-threading scripts are taking the same time when I give as input a big number like 555550000

单线程

import threading, time
a=[]
def print_factors(x):
   for i in range(1, x + 1):
       if x % i == 0:
           a.append(i)

n=int(input("Please enter a large number"))
print ("Starting time is %s" % ( time.ctime(time.time()) ))
print("The factors of",n,"are:")
thread = threading.Thread(target=print_factors,args=(n,))
thread.start()
thread.join()
print("Finishing time is %s" % (time.ctime(time.time())))
print(a)

多线程

import threading, time
a=[]
def print_factors1(x):
   for i in range(1, int(x/2)):
       if x % i == 0:
           a.append(i)

def print_factors2(x):
    for i in range(int(x/2), x+1):
        if x % i == 0:
            a.append(i)

n=int(input("Please enter a large number"))
print ("Starting time is %s" % ( time.ctime(time.time()) ))
thread1 = threading.Thread(target=print_factors1,args=(n,))
thread2 = threading.Thread(target=print_factors2,args=(n,))
print("The factors of",n,"are:")
thread1.start()
thread2.start()
thread2.join()
print("Finishing time is %s" % (time.ctime(time.time())))
print(a)

我试图了解单线程和多线程在获得结果所需的时间方面的区别.
我正在测量两种类型的相似时间,但我无法找出原因.

I am trying to understand the difference between single-threading and multi-threading in terms of time taken to got the results.
I'm measuring similar timings for both types and I cannot figuring out the reasons.

推荐答案

你的问题是 GIL,全局解释器锁.

Your problem is GIL, the Global Interpreter Lock.

Python 全局解释器锁或 GIL,简单来说,就是一个互斥锁(或锁)只允许一个线程控制Python 解释器.

The Python Global Interpreter Lock or GIL, in simple words, is a mutex (or a lock) that allows only one thread to hold the control of the Python interpreter.

您可以在此处找到有关 GIL 的详细信息(只需在 Google 上快速搜索即可找到更多来源):

You can found detailed informations about GIL here (just a fast search on Google and you can find a lot more sources):

您需要更改您的实现以使用进程而不是线程.
我按如下方式更改了您的脚本:

You need to change your implementation to use processes instead of threads.
I changed your script as follows:

from multiprocessing import Pool
import time
def print_factors1(x):
    a=[]
    for i in range(1, int(x/2)):
        if x % i == 0:
            a.append(i)
    return a

def print_factors2(x):
    a=[]
    for i in range(int(x/2), x+1):
        if x % i == 0:
            a.append(i)
    return a

if __name__ == '__main__':
    n=int(input("Please enter a large number"))
    pool = Pool(processes=2)
    print ("Starting time is %s" % ( time.ctime(time.time()) ))

    process1 = pool.apply_async(print_factors1,[n])
    process2 = pool.apply_async(print_factors2,[n])

    pool.close()
    pool.join()

    print("Finishing time is %s" % (time.ctime(time.time())))
    print("The factors of",n,"are:")
    print(process1.get())
    print(process2.get())

考虑到线程共享内存,进程.

Take into account that threads share the memory, processes don't.

这篇关于CPU 密集型任务中的多线程和单线程性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆