python - 了解 Python 配置文件输出

标签 python profiling profile

我正在尝试使用 Python 分析器来加速我的代码。我已经能够确定几乎所有时间都花在了哪个特定函数上,但我无法弄清楚时间花在了那个函数的什么地方。

下面是配置文件输出,显示“appendBallot”是罪魁祸首,耗时将近 116 秒。在下面,我有“appendBallot”的代码。

我无法从配置文件输出中找出我需要优化“appendBallot”的哪一部分,因为下一个最高时间条目不到一秒。我相信你们中的许多人可以仅从我的代码中告诉我,但我想了解如何从配置文件输出中获取该信息。任何帮助将不胜感激。

配置文件输出:

  ncalls  tottime  percall  cumtime  percall filename:lineno(function)
       1    0.000    0.000  116.168  116.168 <string>:1(<module>)
       1    0.001    0.001  116.168  116.168 {execfile}
       1    0.003    0.003  116.167  116.167 foo.py:1(<module>)
       1    0.000    0.000  116.139  116.139 ballots.py:330(loadKnown)
       1    0.000    0.000  116.109  116.109 plugins.py:148(load)
       1    0.196    0.196  116.108  116.108 BltBallotLoader.py:37(loadFile)
  100000  114.937    0.001  115.912    0.001 ballots.py:133(appendBallot)
  100000    0.480    0.000    0.790    0.000 ballots.py:117(newBallot)
  316668    0.227    0.000    0.310    0.000 ballots.py:107(getNumCandidates)
417310/417273    0.111    0.000    0.111    0.000 {len}
  200510    0.071    0.000    0.071    0.000 {method 'append' of 'list' objects}
   99996    0.045    0.000    0.045    0.000 {method 'add' of 'set' objects}
  100000    0.042    0.000    0.042    0.000 {method 'has_key' of 'dict' objects}
       1    0.000    0.000    0.030    0.030 plugins.py:202(getLoaderPluginClasses)
       1    0.000    0.000    0.030    0.030 plugins.py:179(getPluginClasses)
       1    0.000    0.000    0.030    0.030 plugins.py:205(getLoaderPluginClass)
       3    0.016    0.005    0.029    0.010 {__import__}
       1    0.022    0.022    0.025    0.025 ballots.py:1(<module>)
       1    0.010    0.010    0.013    0.013 BltBallotLoader.py:1(<module>)
       7    0.000    0.000    0.003    0.000 re.py:227(_compile)

代码:

  def appendBallot(self, ballot, ballotID=None):
    "Append a ballot to this Ballots object."

    # String representation of ballot for determining whether ballot is unique
    ballotString = str(list(ballot))

    # Ballot as the appropriate array to conserve memory
    ballot = self.newBallot(ballot)

    # Assign a ballot ID if one has not been given
    if ballotID is None:
      ballotID = len(self.ballotIDs)
    assert(ballotID not in self.ballotIDs)
    self.ballotIDs.append(ballotID)

    # Check to see if we have seen this ballot before
    if self.uniqueBallotsLookup.has_key(ballotString):
      i = self.uniqueBallotsLookup[ballotString]
      self.uniqueBallotIDs[i].add(ballotID)
    else:
      i = len(self.uniqueBallots)
      self.uniqueBallotsLookup[ballotString] = i
      self.uniqueBallots.append(ballot)
      self.uniqueBallotIDs.append(set([ballotID]))
    self.ballotOrder.append(i)

最佳答案

是的,我也遇到了同样的问题。

我知道解决此问题的唯一方法是将大型函数包装到几个较小的函数调用中。这将允许探查器考虑每个较小的函数调用。

有趣的是,这样做的过程(无论如何对我来说)让低效之处一目了然,所以我什至不必运行分析器。

关于python - 了解 Python 配置文件输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1469679/

相关文章:

python - 在 TSNE 中选择 random_State 参数(python)

google-chrome-extension - 从 Javascript 触发堆分析?

python - 我如何猴子修补 PyQT 的 QApplication.notify() 来计时事件

java - Spring 配置文件和测试

c++ - 如何在 C++ 中以编程方式解析 %UserProfile% 的位置?

python - Pandas - 在分类数据中填充 NaN

python - pyqt中的异步模式?或者更干净的后台调用模式?

python - 将带有破折号的列表转换为数字

c++ - Cachegrind:为什么有这么多缓存未命中?

maven-2 - 您知道 mvnrepository.com 的 Maven 配置文件吗?