记录导入模块所需的时间 [英] Record time taken to import module

查看:134
本文介绍了记录导入模块所需的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关背景资料:请参阅这里

For background information: Go HERE!

我有一个非常大的模块,可以从互联网上获取东西,其他内置脚本等等。根据网络速度等因素,内存然后编译列表和类似的东西,导入时间可以在25秒到90秒之间变化。我使用以下代码来跟踪模块导入的时间:

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)))

当我运行时:

>>> 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?

推荐答案

通常不希望在模块导入时进行大量工作 - 这会对文档扫描程序造成严重破坏,IDE,单元测试框架等。理想情况下,应该重写 tms 以在函数中完成其工作。但要解决您的问题,只需编写一个导入模块的简短模块。您甚至可以将其命名为 tms 并重命名原件,以便其他进口商获得相同的功能(如果需要的话)。

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)))

现在只需导入tmsx

Now just import tmsx

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

这篇关于记录导入模块所需的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆