Python 单核多核与多线程效率对比

Published on 2017 - 10 - 11

在使用多线程与多进程的时候,常常在想,如果只是单纯的进行运算,到底是使用threading的多线程快呢,还是使用multiprocessing的多核心快呢?这两个与单线程单核心相比效率又相差多少呢?今天突然想做个对比,于是写了代码,让我们一起来看看结果吧。

代码编写

import multiprocessing, time, threading

def job(q):
    res = 0
    for i in range(1000000):
    res += i + i ** 2 + i ** 3
    q.put(res)


def normal():
    res = 0
    for x in range (4):
    for i in range(1000000):
        res += i + i ** 2 + i ** 3
    print('normal',res)


def multi_threads():
    q = multiprocessing.Queue()
    t1 = threading.Thread(target=job, args=(q,))
    t2 = threading.Thread(target=job, args=(q,))
    t3 = threading.Thread(target=job, args=(q,))
    t4 = threading.Thread(target=job, args=(q,))
    t1.start()
    t2.start()
    t3.start()
    t4.start()
    t1.join()
    t2.join()
    t3.join()
    t4.join()
    res1 = q.get()
    res2 = q.get()
    res3 = q.get()
    res4 = q.get()
    print('multi_threads:',res1+res2+res3+res4)

def multi_process():
    q = multiprocessing.Queue()
    p1 = multiprocessing.Process(target=job,args=(q,))
    p2 = multiprocessing.Process(target=job,args=(q,))
    p3 = multiprocessing.Process(target=job, args=(q,))
    p4 = multiprocessing.Process(target=job, args=(q,))
    p1.start()
    p2.start()
    p3.start()
    p4.start()
    p1.join()
    p2.join()
    p3.join()
    p4.join()
    res1 = q.get()
    res2 = q.get()
    res3 = q.get()
    res4 = q.get()
    print('mutil_process:',res1+res2+res3+res4)

if __name__ == '__main__':
    st=time.time()
    normal()
    st1=time.time()
    print('normal_time:',st1 - st)

    st=time.time()
    multi_threads()
    st1=time.time()
    print('multiThread_time:',st1 - st)

    st=time.time()
    multi_process()
    st1=time.time()
    print('multilProcess_time:',st1 - st)

结果

代码写好了,让我们来运行看一下结果:

normal 999999333334333332000000
normal_time: 2.4837470054626465

multi_threads: 999999333334333332000000
multiThread_time: 4.1792871952056885

mutil_process: 999999333334333332000000
multilProcess_time: 1.2491023540496826

从结果中,我们神奇的发现,效率是多核最快,相比单纯的多线程速度快了将近四倍,而且单核单线程,竟然比多线程的速度还要快。