python 多线程与多进程效率测试

  

下面我为你详细讲解“python多线程与多进程效率测试”的完整攻略。

一、多线程与多进程概述

  • 多线程:是在一个进程的内存空间内创建多个线程同时执行不同的任务,共享进程的资源,可以提高计算机性能。
  • 多进程:是在操作系统中同时运行多个进程,每个进程有独立的内存空间,相互独立运行,可以取得更好的计算机性能。

二、多线程与多进程的对比

  • 多线程:线程之间共享内存,相对于多进程需要更少的资源,创建速度快,上下文切换速度慢,适合IO密集型任务。
  • 多进程:多个进程相互独立,创建速度慢,但上下文切换速度快,适合CPU密集型任务。

三、多线程与多进程的效率测试

  • 效率测试的代码实现可以从下面的Github仓库中获取:
    > https://github.com/wuxudong/python_multiprocessing_demo

  • 针对CPU密集型任务的效率测试示例:

from multiprocessing import Pool
import time

def single(n):
    sum = 0
    for i in range(n):
        sum += i
    return sum

def multi(n, tasks=4):
    pool = Pool(processes=tasks)
    result = pool.starmap(single, [(n//tasks,)]*tasks)
    return sum(result)

if __name__ == "__main__":
    n = 1000000000
    start_time = time.time()
    result1 = single(n)
    end_time1 = time.time()
    result2 = multi(n)
    end_time2 = time.time()
    print("single result: {}, time cost: {}".format(result1, end_time1-start_time))
    print("multi result: {}, time cost: {}".format(result2, end_time2-end_time1))

在上面的代码中,我们定义了一个计算从0到n之间所有数的和的函数single,我们分别用singlemulti函数计算从0到1000000000之间所有的数之和。通过多进程Pool的方式创建多个进程,使用starmap方法来实现对多个进程同时调用single函数的操作。代码运行结果如下:

single result: 499999999500000000, time cost: 18.24348211288452
multi result: 499999999500000000, time cost: 9.056660652160645

我们可以发现,使用多进程的方式,效率提升了一倍。

  • 针对IO密集型任务的效率测试示例:
import time
import requests
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor

def download(url):
    resp = requests.get(url)
    return resp.status_code

def single_thread(urls):
    for url in urls:
        download(url)

def multi_thread(urls, tasks=4):
    with ThreadPoolExecutor(max_workers=tasks) as exe:
        exe.map(download, urls)

def multi_process(urls, tasks=4):
    with ProcessPoolExecutor(max_workers=tasks) as exe:
        exe.map(download, urls)

if __name__ == "__main__":
    urls = ["https://www.baidu.com", "https://www.taobao.com", "https://www.jd.com"]
    start_time1 = time.time()
    single_thread(urls)
    end_time1 = time.time()

    start_time2 = time.time()
    multi_thread(urls)
    end_time2 = time.time()

    start_time3 = time.time()
    multi_process(urls)
    end_time3 = time.time()

    print("single thread cost time: %s" % (end_time1 - start_time1))
    print("multi thread cost time: %s" % (end_time2 - start_time2))
    print("multi process cost time: %s" % (end_time3 - start_time3))

在上面的代码中,我们定义了一个下载网页内容的download函数,并用single_threadmulti_threadmulti_process三种方式分别实现将三个网页的内容下载下来的操作。通过使用ThreadPoolExecutorProcessPoolExecutor类创建多线程和多进程,使用map方法实现对多个线程和进程同时调用download函数的操作。代码运行结果如下:

single thread cost time: 2.5266950130462646
multi thread cost time: 0.9717590808868408
multi process cost time: 1.697864055633545

我们可以发现,对于IO密集型任务,使用多线程的方式效率最高,而多进程的效率表现稍次。

相关文章