04 多进程和多线程的效率对比

import time
from threading import Thread
from multiprocessing import Process


def f1():
    # time.sleep(1)  #io密集型
    # 计算型:
    n = 10
    for i in range(10000000):
        n = n + i

if __name__ == '__main__':
    #查看一下20个线程执行20个任务的执行时间
    t_s_time = time.time()
    t_list = []
    for i in range(5):
        t = Thread(target=f1,)
        t.start()
        t_list.append(t)
    [tt.join() for tt in t_list]
    t_e_time = time.time()
    t_dif_time = t_e_time - t_s_time
    #查看一下20个进程执行同样的任务的执行时间
    p_s_time = time.time()
    p_list = []
    for i in range(5):
        p = Process(target=f1,)
        p.start()
        p_list.append(p)
    [pp.join() for pp in p_list]
    p_e_time = time.time()
    p_dif_time = p_e_time - p_s_time
    print('多线程的执行时间:',t_dif_time)
    print('多jincheng的执行时间:',p_dif_time)

  

转载于:https://www.cnblogs.com/work14/p/10267174.html

你可能感兴趣的:(04 多进程和多线程的效率对比)