导入Python模块是否会影响性能? [英] Does importing a Python module affect performance?

查看:86
本文介绍了导入Python模块是否会影响性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在寻找解决方案时,通常会遇到几种方法.我经常使用最符合我所熟悉的语法的解决方案.但是有时最受支持的解决方案涉及到导入一个新模块,例如该线程.

When searching for a solution, it's common to come across several methods. I often use the solution that most closely aligns with syntax I'm familiar with. But sometimes the far-and-away most upvoted solution involves importing a module new to me, as in this thread.

我已经用大型脚本导入了各种模块,这些模块将循环50K次.导入其他模块会影响处理时间,还是会影响脚本的效率?我是否需要担心要调用的模块的大小?在一般情况下,是否值得花额外的时间/精力使用我已经在使用的模块中包含的方法来寻找解决方案,以寻求指导.

I'm already importing various modules in large script that will be looping 50K times. Does importing additional modules affect processing time, or otherwise affect the script's efficiency? Do I need to worry about the size of the module being called? Seeking guidance on whether, generally, it's worth the extra time/effort to find solutions using methods contained in modules I'm already using.

推荐答案

Python中的每个字节码都会影响性能.但是,除非该代码位于关键路径上并且重复了很多次,否则效果是如此之小以至于无所谓.

Every bytecode in Python affects performance. However, unless that code is on a critical path and repeated a high number of times, the effect is so small as to not matter.

使用import包含两个不同的步骤:加载模块(仅完成一次)和绑定名称(将导入的名称添加到名称空间中以引用模块加载的内容) ,或模块对象本身).绑定名称几乎是没有成本的.由于加载模块仅发生一次,因此不会影响您的性能.

Using import consists of two distinct steps: loading the module (done just once), and binding names (where the imported name is added to your namespace to refer to something loaded by the module, or the module object itself). Binding names is almost costless. Because loading a module happens just once, it won't affect your performance.

将重点放在模块功能可以帮助您有效解决问题的功能上.

Focus instead on what the module functionality can do to help you solve your problem efficiently.

这篇关于导入Python模块是否会影响性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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