zoukankan      html  css  js  c++  java
  • python高级模块concurrent 进程与线程效率对比

    最近项目中用到了多进程来算模型的特征,看看python高级库的多进程与多线程的效率

     1 from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
     2 from concurrent import futures
     3 import time
     4 
     5 
     6 class Fast(object):
     7 
     8     @staticmethod
     9     def get_data(seq):
    10 
    11         time.sleep(0.02)
    12         return seq
    13 
    14 
    15 if __name__ == '__main__':
    16     data_list_p = []
    17     data_list_t = []
    18     data_list_ = []
    19 
    20     # 进程
    21     def main_p():
    22         fast_p = Fast()
    23         with ProcessPoolExecutor(max_workers=8) as executor:
    24             future = [executor.submit(fast_p.get_data, i) for i in range(100)]
    25             for i in futures.as_completed(future):
    26                 data_list_p.append(i.result())
    27         print("进程", data_list_p)
    28     start = time.time()
    29     main_p()
    30     end = time.time()
    31     print(end - start, "进程")
    32 
    33     # 线程
    34     def main_t():
    35         fast_t = Fast()
    36         with ThreadPoolExecutor(max_workers=8) as executor:
    37             future = [executor.submit(fast_t.get_data, i) for i in range(100)]
    38             futures.as_completed(future)
    39             for i in futures.as_completed(future):
    40                 data_list_t.append(i.result())
    41         print("线程", data_list_t)
    42     start_t = time.time()
    43     main_t()
    44     end_t = time.time()
    45     print(end_t - start_t, "线程")
    46 
    47     # 原始
    48     start_ = time.time()
    49     fast = Fast()
    50     [data_list_.append(fast.get_data(i)) for i in range(100)]
    51     print(data_list_)
    52 
    53     end_ = time.time()
    54     print(end_ - start_)

    输出如下:

    小结:io频繁推荐多线程,计算频繁推荐多进程

  • 相关阅读:
    八、比卦
    七、师卦
    六、讼卦
    五、需卦
    力扣-两数之和
    什么是3NF (范式) ?
    SQL事务4个特性
    什么是索引?
    假设把只包含01的数组(如{0,0,1,1,1,0,1,0,0,1})按照升序排序,可以任意交换两个数的位置,请输出最少需要交换的次数。
    找规律并用编程实现如下数列(数值超过10000停止打印) 1,1,2,2,3,2,5,4,8,8
  • 原文地址:https://www.cnblogs.com/tttzqf/p/13047132.html
Copyright © 2011-2022 走看看