Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an import timing monitor #78

Open
itziakos opened this issue Jan 21, 2015 · 1 comment
Open

Add an import timing monitor #78

itziakos opened this issue Jan 21, 2015 · 1 comment

Comments

@itziakos
Copy link
Member

The following example will time imports

import imp
import sys
import timeit


class Loader(object):
    def __init__(self, path):
        self.path = path

    def load_module(self, name):
        if name in sys.modules:
            return sys.modules[name]
        before = timeit.default_timer()
        try:
            module_info = imp.find_module(name, self.path)
            module = imp.load_module(name, *module_info)
        finally:
            after = timeit.default_timer()
            print('{: <50s} {:.5f}s'.format(name, after - before))
        sys.modules[name] = module
        return module


class Finder(object):

    def find_module(self, name, path=None):
        return Loader(path)

sys.meta_path.insert(0, Finder())

It would be nice to have it as a pikos monitor

@itziakos
Copy link
Member Author

code provided by @sjagoe

@itziakos itziakos added this to the Version 0.3 milestone Jan 21, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant