Record time taken to import module

2019-06-03 16:43发布

问题:

For background information: Go HERE!

I have a very large module that takes things from the internet, other built-in scripts and etc. Depending on factors such as network speed, memory and then compiling lists and stuff like that, the import time can vary from between 25 seconds and 90 seconds. I have used the following code to track how long the module takes to import:

def importTime():
    import time
    startTime = time.time()
    import tms              # This is the name of my module
    print("Time taken {}".format(time.time() - startTime)))

When I run this:

>>> importTime()
Loading Module. This may take up to 60 seconds. # This is my module output
Time taken 31.49

This is what I want to have happen:

>>> import tms
Loading Module. This may take up to 60 seconds.
Time taken: 31.49 seconds

Here's my issue. This is a function which I have to define before importing my module. What I need to be able to do is have my module be able to do this upon startup. I've taken a look at this question, but it's the same concept. Does anyone have any ideas?

回答1:

Its usually undesirable to do large amounts of work at module import - that plays havoc with documentation scanners, IDE's, unit test frameworks and the like. Ideally tms should be rewritten to do its work in a function. But to solve your problem, just write a short module that imports your module. You could even name it tms and rename the original so that other importers get the same functionality (if that's desirable).

tmsx.py

import time
startTime = time.time()
from tms import *         # This is the name of my module
print("Time taken {}".format(time.time() - startTime)))

Now just import tmsx

>>> import tmsx
Loading Module. This may take up to 60 seconds.
Time taken: 31.49 seconds


回答2:

You can overload the __import__ function which is called when a module is imported:

import time
import __builtin__

# save the original __import__ function
original_import = __builtin__.__import__

def custom_import(name, globals=None, locals=None, fromlist=None, level=-1):
  startTime = time.time()

  # call original __import__ function
  result = original_import(name, globals, locals, fromlist, level)

  endTime = time.time()
  print('Time used to load module {}: {}'.format(name, endTime - startTime))

  # return result
  return result

# replace builtin __import__ function
__builtin__.__import__ = custom_import