Eu usei o algoritmo para executá-lo em um arquivo em 1.500.000 linhas / palavras dentro de 81.000 palavras com erros ortográficos. Este foi o resultado do perfil:
1.555.756 function calls in 9.196,582 seconds ~2,5 hours
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.077 0.077 9196.582 9196.582 <string>:1(<module>)
1 6.087 6.087 9196.505 9196.505 select_all_spelling_errors.py:23(findRegions)
245644 1.042 0.000 1.042 0.000 sublime.py:550(__init__)
163763 2.559 0.000 2.559 0.000 sublime.py:557(__str__)
245644 1.357 0.000 15.908 0.000 sublime.py:638(__getitem__)
1 0.000 0.000 0.000 0.000 sublime.py:659(clear)
81881 0.611 0.000 56.255 0.001 sublime.py:662(add)
1 0.385 0.385 56.640 56.640 sublime.py:668(add_all)
81882 0.884 0.000 9114.968 0.111 sublime.py:829(run_command)
245646 0.344 0.000 0.344 0.000 sublime.py:832(sel)
1 0.001 0.001 9196.582 9196.582 {built-in method exec}
81881 0.071 0.000 0.071 0.000 {built-in method isinstance}
81882 9114.084 0.111 9114.084 0.111 {built-in method view_run_command}
81881 55.573 0.001 55.573 0.001 {built-in method view_selection_add_region}
1 0.000 0.000 0.000 0.000 {built-in method view_selection_clear}
245644 13.509 0.000 14.551 0.000 {built-in method view_selection_get}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
1 0.000 0.000 0.000 0.000 {method 'values' of 'dict' objects}
Lá podemos ver o culpado que come quase o tempo todo:
81882 9114.084 0.111 9114.084 0.111 {built-in method view_run_command}
Este foi o código-fonte usado:
import sublime
import sublime_plugin
import cProfile
class SelectAllSpellingErrorsCommand(sublime_plugin.TextCommand):
def run(self, edit):
cProfile.runctx( 'findRegions(self, edit)', globals(), locals() )
def findRegions(self, edit):
regionsList = []
while True:
self.view.run_command('next_misspelling')
if self.view.sel()[0] not in regionsList:
regionsList.append( self.view.sel()[0] )
else:
break
self.view.sel().clear()
self.view.sel().add_all( regionsList )