您当前的位置: 首页 >  Python

FPGA硅农

暂无认证

  • 0浏览

    0关注

    282博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

python多进程

FPGA硅农 发布时间:2021-05-10 22:30:02 ,浏览量:0

import math
import datetime
import multiprocessing as mp


def f(x, y):
    for i in range(10000000):
        pass
    return x+y


if __name__ == '__main__':

    param_dict = zip(list(range(100)), list(range(100)))
    print(param_dict)

    start_t = datetime.datetime.now()
    results1=[]
    for x, y in param_dict:
        results1.append(f(x, y))
    print(results1)
    end_t = datetime.datetime.now()
    elapsed_sec = (end_t - start_t).total_seconds()
    print("單进程计算 共消耗: " + "{:.2f}".format(elapsed_sec) + " 秒")

    start_t = datetime.datetime.now()
    num_cores = int(mp.cpu_count())
    print("本地计算机有: " + str(num_cores) + " 核心")
    pool = mp.Pool(num_cores)
    param_dict = zip(list(range(100)), list(range(100)))
    print(param_dict)
    results2 = [pool.apply_async(f, args=(x,y)) for x,y in param_dict]
    results2 = [p.get() for p in results2]
    print(results2)
    end_t = datetime.datetime.now()
    elapsed_sec = (end_t - start_t).total_seconds()
    print("多进程计算 共消耗: " + "{:.2f}".format(elapsed_sec) + " 秒")


result: 在这里插入图片描述

关注
打赏
1658642721
查看更多评论
立即登录/注册

微信扫码登录

0.0442s