virtualvinodh / aksharamukha-python

Aksharamukha Python Library
GNU Affero General Public License v3.0
43 stars 15 forks source link

Use LRU cache and speed up. #3

Closed vvasuki closed 1 year ago

vvasuki commented 2 years ago

In case of multiple calls, aksharamukha is too slow.

Example:

Lines: 3%|▎ | 11175/353348 [13:32<7:01:17, 13.54it/s]

I believe this can be resolved by using an LRU cache in case of costly operations like:

    with open(dir_path + "/yaml/aksharamukha-scripts.yaml", 'r') as stream:
        data_loaded = yaml.safe_load(stream)

Tip: if this were a function, you could have used a decorator like https://docs.python.org/3/library/functools.html

vvasuki commented 2 years ago

A confirmed fix you can copy paste:

@functools.cache
def _load_data():
    import os
    dir_path = os.path.dirname(os.path.realpath(__file__))
    with open(dir_path + "/yaml/aksharamukha-scripts.yaml", 'r') as stream:
        data_loaded = yaml.safe_load(stream)
    return data_loaded

def convert_default(src, tgt, txt, nativize = True, post_options = [], pre_options = []):
    data_loaded = _load_data()

So much better:

Lines: 7%|▋ | 26193/353348 [00:48<08:22, 650.91it/s]

virtualvinodh commented 2 years ago

Sweet. Thanks for this. I'll implement and push the changes.

V

virtualvinodh commented 1 year ago

This will be pushed in the next update.