python計算密集型任務多程序最佳使用的核數
阿新 • • 發佈:2019-02-10
import time from multiprocessing import Pool from tqdm import tqdm import pandas as pd import matplotlib.pyplot as plt def run(i): count=0 for j in range(100000): count+=j*j return count def test(cpu_count): main_start = time.time() #記錄主程序開始的時間 with Pool(cpu_count) as p: #results = p.map(self.run, range(params_list_len)) results = list(p.map(run, range(1000))) main_end = time.time() #記錄主程序結束時間 return main_end-main_start cup_time_list=[] for i in range(1,25): print(i) now_time=test(i) cup_time_list.append([i,now_time]) cpu_time=pd.DataFrame(cup_time_list) plt.plot(list(cpu_time[0]),list(cpu_time[1])) plt.title("the relationship between the cup_num and computing time consume",color='red') plt.show()
在本地的4核的ubuntu上執行的結果:
在伺服器上,12核的運算結果如下:
非嚴謹結論:在4核的情況下,跑兩個程序是最快的,在12核的情況下,跑6個程序是最快的。都是理論的程序數的一半,難道是因為這兩臺電腦真正的核數分別是2,和6,4和12是從真正核切割出來的?懵逼中