我目睹了 PyCharm 的一些奇怪的运行时问题,解释如下。该代码已在具有 20 个内核和 256 GB RAM 的机器上运行,并且有足够的空闲内存。我没有展示任何实际功能,因为它是一个相当大的项目,但我非常乐意根据要求添加详细信息。
简而言之,我有一个.py
文件项目具有以下结构:
import ...
import ...
cpu_cores = control_parameters.cpu_cores
prng = RandomState(123)
def collect_results(result_list):
return pd.DataFrame({'start_time': result_list[0::4],
'arrival_time': result_list[1::4],
'tour_id': result_list[2::4],
'trip_id': result_list[3::4]})
if __name__ == '__main__':
# Run the serial code
st = starttimes.StartTimesCreate(prng)
temp_df, two_trips_df, time_dist_arr = st.run()
# Prepare the dataframe to sample start times. Create groups from the input dataframe
temp_df1 = st.prepare_two_trips_more_df(temp_df, two_trips_df)
validation.logger.info("Dataframe prepared for multiprocessing")
grp_list = []
for name, group in temp_df1.groupby('tour_id'): ### problem lies here in runtimes
grp_list.append(group)
validation.logger.info("All groups have been prepared for multiprocessing, "
"for a total of %s groups" %len(grp_list))
################ PARALLEL CODE BELOW #################
The for loop在 1050 万行和 18 列的数据帧上运行。按照目前的形式,大约需要25 mins创建组列表(280 万个组)。创建这些组,然后将其提供给多进程池,其代码未显示。
25分钟是相当长的,因为我也做了下面的测试运行,只需要7 mins。本质上,我保存了temp_df1文件到 CSV,然后在预先保存的文件中进行批处理并运行相同的for loop像之前一样。
import ...
import ...
cpu_cores = control_parameters.cpu_cores
prng = RandomState(123)
def collect_results(result_list):
return pd.DataFrame({'start_time': result_list[0::4],
'arrival_time': result_list[1::4],
'tour_id': result_list[2::4],
'trip_id': result_list[3::4]})
if __name__ == '__main__':
# Run the serial code
st = starttimes.StartTimesCreate(prng)
temp_df1 = pd.read_csv(r"c:\\...\\temp_df1.csv")
time_dist = pd.read_csv(r"c:\\...\\start_time_distribution_treso_1.csv")
time_dist_arr = np.array(time_dist.to_records())
grp_list = []
for name, group in temp_df1.groupby('tour_id'):
grp_list.append(group)
validation.logger.info("All groups have been prepared for multiprocessing, "
"for a total of %s groups" %len(grp_list))
QUESTION那么,与将文件作为上游函数的一部分创建时相比,当我只是批处理文件时,是什么导致代码运行速度快了 3 倍呢?
预先感谢,请让我知道如何进一步澄清。