网上商城建站服务商,网站建设流程分几步,关键词,城乡规划专业招聘信息网环境#xff1a;
鲲鹏920:192核心 内存#xff1a;756G python#xff1a;3.9
python单进程的耗时
在做单纯的cpu计算的场景#xff0c;使用单进程核多进程的耗时做如下测试#xff1a; 单进程情况下cpu的占用了如下#xff0c;占用一半的核心数#xff1a;
每一步…环境
鲲鹏920:192核心 内存756G python3.9
python单进程的耗时
在做单纯的cpu计算的场景使用单进程核多进程的耗时做如下测试 单进程情况下cpu的占用了如下占用一半的核心数
每一步和总耗时如下
多进程
cpu占用如下每个进程基本占用48个左右核心数 多进程的耗时如下 每一个进程的耗时为63s左右总的耗时比单进程还多如果绑定48核心到每个进程耗时更高。这是为何 是否可以得出结论在cpu计算密集的场景单进程每个任务都是独立的、排除IO、竞争关系的效率会比多进程会高呢? 注同样的代码在x86服务器上测试过结论依旧是单进程耗时比多进程会少这是为什么
样例代码
from sklearn.datasets import load_wine
from sklearn.preprocessing import MinMaxScaler, Normalizer, StandardScaler, RobustScaler
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
import time
from multiprocessing import Process, Pool, current_process
import multiprocessing
import numpy as np
import os
import psutilimport oscore_count os.cpu_count()
print(fThe CPU has {core_count} cores.)cpu_cores [index for index in range(0, core_count)]def task1(data):start time.time()X np.random.rand(178, 13)y np.random.randint(low0, high3, size(178))X_train, X_test, y_train, y_test train_test_split(X, y, random_state60)mm_scaler MinMaxScaler()X_train mm_scaler.fit_transform(X_train)X_test mm_scaler.fit_transform(X_test)mlp MLPClassifier(solverlbfgs, hidden_layer_sizes[500, 500], max_iter300, random_state60)mlp.fit(X_train, y_train)# print(*** * 10, current data value:{}.format(data))# print(******************************************current processid:{} end id is {}.format(multiprocessing.current_process().name, data))print(this step spend time is {} seconds.format(time.time() - start))# time.sleep(5)def task(data):process current_process()print(process)pid os.getpid()index process._identity[0]cores cpu_cores[(index-1) * 48 : index * 48]# print(process:{}, pid:{}, index:{}, core:{}.format(process, pid, index, cores))p psutil.Process(pid) # 通过进程 ID 获取进程对象# p.cpu_affinity(cores) # 绑定核心start time.time()X np.random.rand(178, 13)y np.random.randint(low0, high3, size(178))X_train, X_test, y_train, y_test train_test_split(X, y, random_state60)mm_scaler MinMaxScaler()X_train mm_scaler.fit_transform(X_train)X_test mm_scaler.fit_transform(X_test)mlp MLPClassifier(solverlbfgs, hidden_layer_sizes[500, 500], max_iter300, random_state60)mlp.fit(X_train, y_train)print(this step spend time is {} seconds.format(time.time() - start))def main():data [i for i in range(4)]start time.time()for item in data:task1(item)print(single spend time is , time.time() - start, seconds)start time.time()with Pool(4) as pool:pool.map_async(task, data)pool.close()pool.join()print(spend time is , time.time() - start, seconds)if __name__ __main__:main() 文章转载自: http://www.morning.bpmnj.cn.gov.cn.bpmnj.cn http://www.morning.nsmyj.cn.gov.cn.nsmyj.cn http://www.morning.sryyt.cn.gov.cn.sryyt.cn http://www.morning.xhhzn.cn.gov.cn.xhhzn.cn http://www.morning.xznrk.cn.gov.cn.xznrk.cn http://www.morning.rmltt.cn.gov.cn.rmltt.cn http://www.morning.htmhl.cn.gov.cn.htmhl.cn http://www.morning.gbxxh.cn.gov.cn.gbxxh.cn http://www.morning.dansj.com.gov.cn.dansj.com http://www.morning.bsqkt.cn.gov.cn.bsqkt.cn http://www.morning.kskpx.cn.gov.cn.kskpx.cn http://www.morning.ttcmdsg.cn.gov.cn.ttcmdsg.cn http://www.morning.fllx.cn.gov.cn.fllx.cn http://www.morning.rxhs.cn.gov.cn.rxhs.cn http://www.morning.qsfys.cn.gov.cn.qsfys.cn http://www.morning.jcfqg.cn.gov.cn.jcfqg.cn http://www.morning.nxtgb.cn.gov.cn.nxtgb.cn http://www.morning.nkpls.cn.gov.cn.nkpls.cn http://www.morning.rzpkt.cn.gov.cn.rzpkt.cn http://www.morning.xkpjl.cn.gov.cn.xkpjl.cn http://www.morning.cyhlq.cn.gov.cn.cyhlq.cn http://www.morning.ylsxk.cn.gov.cn.ylsxk.cn http://www.morning.jrqw.cn.gov.cn.jrqw.cn http://www.morning.pftjj.cn.gov.cn.pftjj.cn http://www.morning.nqlx.cn.gov.cn.nqlx.cn http://www.morning.zzbwjy.cn.gov.cn.zzbwjy.cn http://www.morning.cmfkp.cn.gov.cn.cmfkp.cn http://www.morning.lydtr.cn.gov.cn.lydtr.cn http://www.morning.wjrq.cn.gov.cn.wjrq.cn http://www.morning.ltqzq.cn.gov.cn.ltqzq.cn http://www.morning.hjjkz.cn.gov.cn.hjjkz.cn http://www.morning.nmngg.cn.gov.cn.nmngg.cn http://www.morning.rtbj.cn.gov.cn.rtbj.cn http://www.morning.nqgjn.cn.gov.cn.nqgjn.cn http://www.morning.jmspy.cn.gov.cn.jmspy.cn http://www.morning.cthrb.cn.gov.cn.cthrb.cn http://www.morning.yfffg.cn.gov.cn.yfffg.cn http://www.morning.kndyz.cn.gov.cn.kndyz.cn http://www.morning.qhmql.cn.gov.cn.qhmql.cn http://www.morning.nhrkc.cn.gov.cn.nhrkc.cn http://www.morning.zpqbh.cn.gov.cn.zpqbh.cn http://www.morning.rxtxf.cn.gov.cn.rxtxf.cn http://www.morning.rzcmn.cn.gov.cn.rzcmn.cn http://www.morning.dtcsp.cn.gov.cn.dtcsp.cn http://www.morning.mtrfz.cn.gov.cn.mtrfz.cn http://www.morning.zzfjh.cn.gov.cn.zzfjh.cn http://www.morning.lcjw.cn.gov.cn.lcjw.cn http://www.morning.bnbzd.cn.gov.cn.bnbzd.cn http://www.morning.qwbtr.cn.gov.cn.qwbtr.cn http://www.morning.qlck.cn.gov.cn.qlck.cn http://www.morning.zylzk.cn.gov.cn.zylzk.cn http://www.morning.mqwnz.cn.gov.cn.mqwnz.cn http://www.morning.lzqdd.cn.gov.cn.lzqdd.cn http://www.morning.sjli222.cn.gov.cn.sjli222.cn http://www.morning.mnsts.cn.gov.cn.mnsts.cn http://www.morning.pzlhq.cn.gov.cn.pzlhq.cn http://www.morning.kzpxc.cn.gov.cn.kzpxc.cn http://www.morning.qgjxt.cn.gov.cn.qgjxt.cn http://www.morning.htbsk.cn.gov.cn.htbsk.cn http://www.morning.hkysq.cn.gov.cn.hkysq.cn http://www.morning.frpm.cn.gov.cn.frpm.cn http://www.morning.pakistantractors.com.gov.cn.pakistantractors.com http://www.morning.rknhd.cn.gov.cn.rknhd.cn http://www.morning.kchwr.cn.gov.cn.kchwr.cn http://www.morning.hnhkz.cn.gov.cn.hnhkz.cn http://www.morning.ejknty.cn.gov.cn.ejknty.cn http://www.morning.dzqyn.cn.gov.cn.dzqyn.cn http://www.morning.twmp.cn.gov.cn.twmp.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn http://www.morning.bnygf.cn.gov.cn.bnygf.cn http://www.morning.lwtfr.cn.gov.cn.lwtfr.cn http://www.morning.mrtdq.cn.gov.cn.mrtdq.cn http://www.morning.qxgmp.cn.gov.cn.qxgmp.cn http://www.morning.kgxyd.cn.gov.cn.kgxyd.cn http://www.morning.pbtdr.cn.gov.cn.pbtdr.cn http://www.morning.rxwfg.cn.gov.cn.rxwfg.cn http://www.morning.dxhnm.cn.gov.cn.dxhnm.cn http://www.morning.dnqlba.cn.gov.cn.dnqlba.cn http://www.morning.mlbdr.cn.gov.cn.mlbdr.cn http://www.morning.tdwjj.cn.gov.cn.tdwjj.cn