简体   繁体   English

加速Python进口

[英]Speeding up Python Imports

I have a large program that is structured using object oriented techniques, and I have one main driver module that imports a bunch of other classes, which they in turn import more python built-in modules or other classes. 我有一个使用面向对象技术构建的大型程序,我有一个主要的驱动程序模块,它导入了许多其他类,它们又导入了更多的python内置模块或其他类。 All together I would say there is well over 250 from x import y statements (I don't have control over this part of the code), some are duplicates in other classes and some are unique. 总之,我会说from x import y statements有超过250 from x import y statements (我无法控制这部分代码),有些在其他类中是重复的,有些是唯一的。 I profiled my code and as I suspected the majority of the time is in the start up with import so many modules and classes. 我描述了我的代码,因为我怀疑大部分时间是在启动时导入这么多的模块和类。 There is a way to speed up this? 有办法加快这个吗?

You can move the import statements to inside your functions to reduce initial startup. 您可以将import语句移动到函数内部以减少初始启动。

Another technique would be to copy your modules to a ramdisk or tmpfs for faster IO (these use RAM, which runs much faster). 另一种技术是将模块复制到ramdisk或tmpfs以获得更快的IO(这些使用RAM,运行速度更快)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM