[英]single threaded script is consuming too much cpu on a multi core processor
我编写了一个代码来帮助从非常量数据集中在恒定时间间隔内积累一些数据。 它在我当前的 6 核处理器 (Intel i5-8400) 计算机上按预期工作,并且消耗了大约 25% 的 CPU 使用率。 问题是,现在我试图在另一台具有 40 个内核的计算机(英特尔至强 E5-2630 v4)上重新运行相同的代码,并且一旦它启动以下功能,它就会飙升至 55% CPU 使用率,而我在预计接近5%。
这已经成为一个问题,因为我正在与其他人共享这台计算机,这阻碍了他们的工作。 比较两台计算机之间运行脚本所需的时间,它似乎可以忽略不计,所以我不确定它是否真的使用了所有处理器(这是有道理的,因为我不知道如何多线程循环!)
我以前曾试图找到答案,我能找到的最接近的答案是它可能是由于 while 循环( Python 脚本 CPU 使用过多),但我认为这不适用于我的情况......大多数其他问题与如何增加 CPU 使用率有关,这与我的问题相反。
def accumulate_value (table_data_1,i_time_step):
# table_data_1 formatted as a dataframe with columns names ['value','date_time']
table_data_1.reset_index(drop=True,inplace=True)
table_data_1["delta_time"] = table_data_1["date_time"].diff(periods=1).dt.seconds/60
table_data_1["delta_time_aux"] = 0
table_data_1["value_aux"] = 0
table_data_1["datetime_stamp"] = pd.to_datetime(0)
# Start loop
for aux_counter_1 in table_data_1.index.values.tolist():
table_data_1_aux1 = table_data_1.loc[aux_counter_1:,:]
table_data_3 = table_data_1_aux1.loc[table_data_1_aux1.index[table_data_1_aux1.loc[:,'date_time'] - table_data_1_aux1.loc[aux_counter_1,'date_time'] <= datetime.timedelta(minutes=i_time_step)],:]
if len(table_data_3.index) > 1 :
table_data_1.iloc[aux_counter_1,3] = datetime.timedelta.total_seconds(table_data_3.iloc[-1,1] - table_data_3.iloc[0,1])/60
if table_data_1.iloc[aux_counter_1,3] >= i_time_step:
table_data_1.iloc[aux_counter_1,4] = table_data_3.loc[:,'value'].sum() - table_data_3.iloc[-1,0]
else:
table_data_1.iloc[aux_counter_1,4] = table_data_3.loc[:,'value'].sum()
table_data_1.iloc[aux_counter_1,5] = table_data_3.iloc[-1,1]
elif len(table_data_3.index) == 1 :
table_data_1.iloc[aux_counter_1,3] = 0
table_data_1.iloc[aux_counter_1,4] = table_data_3.loc[:,'value'].sum()
table_data_1.iloc[aux_counter_1,5] = table_data_3.iloc[-1,1]
else:
print(table_data_3)
table_data_1["year_stamp"] = table_data_1["datetime_stamp"].dt.year
table_data_2 = table_data_2
return table_data_2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.