Is there a penalty for using built-in libraries in Python?

I recently thought about standard libraries and used them in my programming. And I need to wonder about calling libraries, I hear a lot of talk about dependencies and managing them so as not to overload your program with unnecessary modules and whatnot. So I was wondering if there is additional load / increase in resource usage when using functions and modules from the standard library.

For example, if I wrote a program that was built entirely from the standard lib functions and none of my "native" code (which means I have a large number of imports), would you see a performance hit? Or is the standard library loaded by every program, whether called or not? Hence, it is part of the standard library.

Thanks guys, I'd love to go over my question if I wasn't clear enough.

+3


source to share


1 answer


The performance impact is minimal.

Importing a module for the first time loads the module bytecode and objects into memory (stored in the mapping sys.modules

). This download will take a small amount of time and a small amount of memory.



You have to be a much larger project to take action. A Mercurial project that cares deeply about startup time (the command line client must be responsive and fast) uses a lazy loading scheme, in which the loading of the loaded module is delayed until it is actually accessed. Thus, a project can reference hundreds of modules (and extensions), but only actually loads those required by the current command line parameters.

An alternative could be your own code to define functionality, but executing the bytecode to do this will also take time and memory, but with the added disadvantage that you are more likely to introduce bugs or design bugs that the standard library has managed to fix over the years. years old.

+6


source







All Articles