Luc*_*tto 15 python performance matplotlib pandas seaborn
我有以下数据框。
In [12]: dfFinal
Out[12]:
module vectime vecvalue
1906 client1.tcp [1.1007512, 1.1015024, 1.1022536, 1.1030048, 1... [0.0007512, 0.0007512, 0.0007512, 0.0007512, 0...
1912 client2.tcp [1.10079784, 1.10159568, 1.10239352, 1.1031913... [0.00079784, 0.00079784, 0.00079784, 0.0007978...
1918 client3.tcp [1.10084448, 1.10168896, 1.10258008, 1.1036111... [0.00084448, 0.00084448, 0.00089112, 0.0010310...
Run Code Online (Sandbox Code Playgroud)
我想为每个模块绘制 timeSeries vecvaluevs。vectime
为此,我可以执行以下操作:
1) Matplotlib
start = datetime.datetime.now()
for row in dfFinal.itertuples():
t = row.vectime
x = row.vecvalue
x = runningAvg(x)
plot(t,x)
total = (datetime.datetime.now() - start).total_seconds()
print("Total time: ",total)
Run Code Online (Sandbox Code Playgroud)
这样做需要0.07005几秒钟才能完成。
2) 海生
start = datetime.datetime.now()
for row in dfFinal.itertuples():
t = row.vectime
x = row.vecvalue
x = runningAvg(x)
DF = pd.DataFrame({'x':x, 't':t})
sns.lineplot(x='t', y='x', data=DF)
total = (datetime.datetime.now() - start).total_seconds()
print("Total time: ",total)
Run Code Online (Sandbox Code Playgroud)
这样做需要19.157463几秒钟才能完成。
为什么会有如此巨大的差异?我做错了什么以至于处理一个相当小的 DF 需要这么长时间?
Ros*_* B. 25
设置ci=None在调用lineplot; 否则,将计算置信区间,导致一些昂贵(和不必要)的df.groupby调用。
旁白:该snakeviz模块是快速查找计算瓶颈的绝佳工具。