python:重新导入模块的成本是多少? [英] python: What is the cost of re-importing modules?

查看:164
本文介绍了python:重新导入模块的成本是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常试图在需要它们的狭窄环境中导入模块。例如,在使用该模块的函数体中。在这种情况下,import语句可能会执行多次。

I am often tempted to import modules in narrow contexts where they are needed. For example in the body of a function that utilises the module. In this case, the import statement may be executed many times.

除了样式问题,这样做的性能成本是多少?

Apart from stylistic issues, what is the performance cost of doing this?

推荐答案

看看这个网站上的解释:

Take a look at the explanation on this site:

https://wiki.python.org/moin/PythonSpeed/PerformanceTips#Import_Statement_Overhead

尽管Python不会多次导入同一个模块,根据代码的结构,您可能仍会遇到性能损失。您可以使用计时器查看实际影响。

Even though Python won't import the same module multiple times, you may still have a performance hit depending on how your code is structured. You may be able to use the Timer to see what the actual impact is.

这篇关于python:重新导入模块的成本是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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