textstat icon indicating copy to clipboard operation
textstat copied to clipboard

Performance suggestions

Open abrom opened this issue 5 years ago • 3 comments

I've noticed some pretty nasty performance issues related to the use of the Text::Hyphen package. When passed in really long words it can take 5+ minutes to 'visualise'. Given there is no memoization of the Text::Hyphen dictionary (and it's caches) then calling each of the various methods that use your syllable_count method can end up taking hours to calculate what is otherwise a pretty simple piece of text (albeit with a few errant 'long' words).

Suggested solution would be to memoize the dictionary. Given that each of the methods in the TextStat class are class methods it does complicate things a little. An option would be to move each of the the class methods into instance methods then create wrappers on the class to create an instance (to keep backward compatibility).

ie

def self.sentence_count(text)
  new.sentence_count text
end

def sentence_count(text)
  ...
end

Of course could also use some meta programming to simplify the various class method calls too.

This would allow the dictionary to be memoized and minimise its cache being flushed, reducing runtime from hours to minutes.

Happy to put together a PR

abrom avatar Jan 22 '20 00:01 abrom

Good points. I know there may be some problems with performance due to the fact that the original python Texstat library utilizes LRU caching.

kupolak avatar Feb 07 '20 17:02 kupolak

Here are the changes i've been using for the past few weeks with great success:

https://github.com/kupolak/textstat/compare/master...Studiosity:cache-dictionary

Note I've been sure to leave the existing interface untouched whilst opening up using the tools with great speed-ups:

text_service = TextStat.new 'ru'
reading_ease = text_service.flesch_reading_ease text
smog_index = text_service.smog_index text
...

Another (slight) improvement would be to pass the text block in through the initialiser to save passing it to each instance method..

Again, happy to put this in a PR if you're interested.

abrom avatar Feb 08 '20 06:02 abrom

PRs welcome.

kupolak avatar Feb 08 '20 17:02 kupolak