【发布时间】:2016-04-06 00:02:16
【问题描述】:
我的程序与 API 交互、执行计算并使用信息构建 Gui,其中一些信息存储在本地文件中(以在登录之间保存信息)。使用 cProfile 分析我的代码时,我得到以下输出:
C:\Users\cheek\Documents\Code\LoL-Performance-Tracker>python -m cProfile -s tottime LoL-Performance-Tracker.py
LoL-Performance-Tracker.py:271: SyntaxWarning: name 'apiKey' is used prior to global declaration
global apiKey
Entered buildMatchHistory
262880 function calls (261634 primitive calls) in 11.867 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
161 8.684 0.054 8.684 0.054 decoder.py:370(raw_decode)
1 1.100 1.100 1.100 1.100 {question}
1 0.694 0.694 1.794 1.794 {built-in method exec_}
1 0.506 0.506 9.848 9.848 LoL-Performance-Tracker.py:88(buildMatchHistory)
168 0.361 0.002 0.361 0.002 {method 'read' of 'file' objects}
84 0.073 0.001 0.149 0.002 ConfigParser.py:464(_read)
1 0.040 0.040 11.867 11.867 LoL-Performance-Tracker.py:7(<module>)
80 0.035 0.000 9.323 0.117 MatchHistoryBuilder.py:37(buildMatch)
161 0.026 0.000 9.074 0.056 __init__.py:258(load)
251 0.025 0.000 0.025 0.000 {open}
1 0.023 0.023 0.023 0.023 {built-in method show}
23338 0.019 0.000 0.019 0.000 {method 'match' of '_sre.SRE_Pattern' objects}
23176 0.017 0.000 0.017 0.000 collections.py:59(__setitem__)
2 0.016 0.008 0.016 0.008 {built-in method setWidget}
84 0.010 0.000 0.010 0.000 {built-in method setStyleSheet}
11844 0.008 0.000 0.008 0.000 {method 'readline' of 'file' objects}
1 0.006 0.006 11.704 11.704 LoL-Performance-Tracker.py:326(main)
...
buildMatchHistory 方法是我认为的问题,因为它构建 gui 对象并且通常非常麻烦,但似乎不是。
我正在使用 json 编码器/解码器来执行包含大量信息的文件 I/O。我不认为执行这些操作需要几秒钟,但看起来确实如此。我对这个输出的理解正确吗?如果没有,我应该去哪里看?
如果我是对的,那么在登录之间提取和存储信息的更好解决方案是什么?
【问题讨论】:
-
您正确解释了结果。您存储了多少文件?如果目标信息分布在许多文件中,您可能会看到通过使用多处理并行读取速度有所提高。
-
分析器输出与代码一起使用会更有帮助。
-
我应该只发布代码还是添加指向 pastebin 的链接?
-
有些人做this 看看什么需要时间。
标签: python python-2.7 io profiler cprofile