描述
在省略号处填写一行或多行代码,完成如下功能。
同时,out.txt,其中有一些数据库操作功能的执行时间信息,如下所示:
示例
starting,0.000037,2.102
After opening tables,0.000008,0.455
System lock,0.000004,0.227
其中第1列是操作的名字,第2列是操作所花费的时间,单位是秒,第3列是操作时间占全部过程的百分比,字段之间用逗号','隔开。
读取out.txt文件里的内容,统计所有操作所花费的时间总和,并输出操作时间百分比最多的三个操作所占百分比的值,及其对应的操作名称,显示在屏幕上, 如下所示: .
示例
the total execute time is 0.001724
the top 0 percentage time is 46.023, spent in "Filling schema table" operation
the top 1 percentage time is 36.932, spent in "Sending data" operation
代码示例:(有问题可以联系我o(* ̄︶ ̄*)o)
sumtime = 0
percls = []
ts = {}
with open('out.txt', 'r') as f:
for line in f:
line=line.strip("\n").split(",")
sumtime+=eval(line[1])
ts[line[0]]=line[2]
print('the total execute time is ', sumtime)
tns = list(ts.items())
tns.sort(key=lambda x: x[1], reverse=True)
for i in range(3):
print('the top {} percentage time is {}, spent in "{}" operation'.format(i, tns[i][1],tns[i][0]))