• <i id='JWS86'><tr id='JWS86'><dt id='JWS86'><q id='JWS86'><span id='JWS86'><b id='JWS86'><form id='JWS86'><ins id='JWS86'></ins><ul id='JWS86'></ul><sub id='JWS86'></sub></form><legend id='JWS86'></legend><bdo id='JWS86'><pre id='JWS86'><center id='JWS86'></center></pre></bdo></b><th id='JWS86'></th></span></q></dt></tr></i><div id='JWS86'><tfoot id='JWS86'></tfoot><dl id='JWS86'><fieldset id='JWS86'></fieldset></dl></div>
    <tfoot id='JWS86'></tfoot>

      1. <small id='JWS86'></small><noframes id='JWS86'>

        • <bdo id='JWS86'></bdo><ul id='JWS86'></ul>
        <legend id='JWS86'><style id='JWS86'><dir id='JWS86'><q id='JWS86'></q></dir></style></legend>
      2. python 多线程与多进程效率测试

        时间:2023-12-16

              <i id='ACPgM'><tr id='ACPgM'><dt id='ACPgM'><q id='ACPgM'><span id='ACPgM'><b id='ACPgM'><form id='ACPgM'><ins id='ACPgM'></ins><ul id='ACPgM'></ul><sub id='ACPgM'></sub></form><legend id='ACPgM'></legend><bdo id='ACPgM'><pre id='ACPgM'><center id='ACPgM'></center></pre></bdo></b><th id='ACPgM'></th></span></q></dt></tr></i><div id='ACPgM'><tfoot id='ACPgM'></tfoot><dl id='ACPgM'><fieldset id='ACPgM'></fieldset></dl></div>
              <legend id='ACPgM'><style id='ACPgM'><dir id='ACPgM'><q id='ACPgM'></q></dir></style></legend>
              • <tfoot id='ACPgM'></tfoot>
                  <bdo id='ACPgM'></bdo><ul id='ACPgM'></ul>
                    <tbody id='ACPgM'></tbody>

                  <small id='ACPgM'></small><noframes id='ACPgM'>

                  下面我为你详细讲解“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密集型任务,使用多线程的方式效率最高,而多进程的效率表现稍次。

                  上一篇:对Python多线程读写文件加锁的实例详解 下一篇:python获取多线程及子线程的返回值

                  相关文章

                    <tfoot id='l5QlS'></tfoot>
                    <i id='l5QlS'><tr id='l5QlS'><dt id='l5QlS'><q id='l5QlS'><span id='l5QlS'><b id='l5QlS'><form id='l5QlS'><ins id='l5QlS'></ins><ul id='l5QlS'></ul><sub id='l5QlS'></sub></form><legend id='l5QlS'></legend><bdo id='l5QlS'><pre id='l5QlS'><center id='l5QlS'></center></pre></bdo></b><th id='l5QlS'></th></span></q></dt></tr></i><div id='l5QlS'><tfoot id='l5QlS'></tfoot><dl id='l5QlS'><fieldset id='l5QlS'></fieldset></dl></div>
                  1. <legend id='l5QlS'><style id='l5QlS'><dir id='l5QlS'><q id='l5QlS'></q></dir></style></legend>

                  2. <small id='l5QlS'></small><noframes id='l5QlS'>

                    • <bdo id='l5QlS'></bdo><ul id='l5QlS'></ul>