Ich versuche den Python-Profiler zu verwenden, um meinen Code zu beschleunigen. Ich konnte die spezifische Funktion identifizieren, bei der fast die gesamte Zeit verbraucht wird, aber ich kann nicht herausfinden, wo in dieser Funktion die Zeit verbracht wird.Python-Profil-Ausgabe verstehen
Unten habe ich die Profilausgabe, die zeigt, dass "appendBallot" der Hauptschuldige ist und verbraucht fast 116 Sekunden. Weiter unten habe ich den Code für "appendBallot".
Ich kann nicht aus der Profilausgabe herausfinden, welchen Teil von "appendBallot" ich optimieren muss, da der nächsthöhere Zeiteintrag weniger als eine Sekunde ist. Ich bin mir sicher, dass viele von Ihnen mir nur aus meinem Code erzählen könnten, aber ich würde gerne verstehen, wie man diese Informationen aus der Profilausgabe holt. Jede Hilfe würde sehr geschätzt werden.
Profil Ausgang:
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)
Code:
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)
Es ist in der Tat die Assert(), die die ganze Zeit hogging. Ich frage mich, ob der Python-Profiler assert() -Anweisungen ignoriert, da sie nicht ausgeführt werden, wenn der Code mit -O, –
, ausgeführt wird. Danke für alle hilfreichen Antworten. –
Der Python-Profiler ignoriert 'assert'/statements nicht mehr als ignoriert alle anderen/Anweisungen/in der Methode. Das Schreiben von 'assert (expression)' anstelle von nur 'assert expression' macht es nicht zu einem Funktionsaufruf, der unigniert werden kann. –