Python Sequencematcher 오버 헤드 -1000% CPU 사용 및 매우 느린 처리
문제
Difflib을 사용하여 파일을 두 디렉토리 (연속 버전)로 비교하고 있습니다. 먼저 FilECMP를 사용하여 변경된 파일을 찾은 다음 Difflib.SequemCatcher를 사용하여 비교하고 설명 된대로 HTML Diff를 생성합니다. 여기.
그러나 프로그램이 실행하는 데 너무 오래 걸리고 Python이 100% CPU를 사용하고 있습니다. 제 시간에 프로파일 링을 찾았습니다 seqm.get_opcodes () 항상 걸리는 전화.
모든 통찰력에 감사드립니다. 감사 !
암호:
#changed_set contains the files to be compared
for i in changed_set:
oldLines = open(old_dir +"/" + i).read()
newLines = open(new_dir +"/" + i).read()
seqm = difflib.SequenceMatcher(lambda(x): x in string.whitespace, oldLines, newLines)
opcodes = seqm.get_opcodes() #XXX: Lots of time spent in this !
produceDiffs(seqm, opcodes)
del seqm
해결책
내 대답은 문제에 대한 다른 접근법입니다. GIT와 같은 버전 제어 시스템을 사용하여 디렉토리가 수년에 걸쳐 어떻게 변경되었는지 조사해보십시오.
첫 번째 디렉토리에서 저장소를 만들고 내년 디렉토리로 컨텐츠를 교체하고 변경으로 커밋하십시오. (또는 .git 디렉토리를 내년 디렉토리로 이동하여 복사/삭제를 저장). 반복하다.
그런 다음 Gitk를 실행하면 트리의 두 가지 수정 사이에서 무엇이 바뀌 었는지 볼 수 있습니다. 이진 파일이 변경되었거나 텍스트 파일의 차이가 있습니다.
다른 팁
당신은 또한 시도 할 수 있습니다 diff-match-patch
도서관, 내 경험상 10 배 더 빠를 수 있습니다.
편집하다: 내 다른 대답을 여기서 예제합니다
from diff_match_patch import diff_match_patch
def compute_similarity_and_diff(text1, text2):
dmp = diff_match_patch()
dmp.Diff_Timeout = 0.0
diff = dmp.diff_main(text1, text2, False)
# similarity
common_text = sum([len(txt) for op, txt in diff if op == 0])
text_length = max(len(text1), len(text2))
sim = common_text / text_length
return sim, diff
제휴하지 않습니다 StackOverflow