I was recently thinking about standard libraries and using them in my programming. And I got to wondering about calling libraries, I hear a lot of talk on dependencies and managing them in order to not overload your program with unnecessary modules and whatnot. So I was wondering if there is additional load/increase in resource use when using functions and modules from the standard library.
For instance, if I wrote a program that was entirely built of standard lib functions and none of my "own" code (meaning that I have a large amount of import statements), would I see a performance dropoff? Or is standard library loaded with every program, regardless of whether it's called or not? Hence it being part of the standard library.
Thanks guys, happy to elaborate on my question if I haven't been clear enough.
The performance impacts are minimal.
Importing a module the first time loads the module bytecode and objects into memory (stored in the sys.modules mapping). That loading will take a small amount of time, and a small amount of memory.
You'd have to be a much larger project for that to start to matter. The Mercurial project, which cares deeply about start-up time (a command line client has to be responsive and fast), uses a lazy-loading scheme where imported module loading is deferred until actually accessed. This way the project can reference hundreds of modules (and extensions) but only actually load those that the current command line options require.
The alternative would be for your own code to define the functionality, but executing the bytecode for that would also take time and memory, but with the added downside that you are likely to introduce bugs or make design mistakes that the standard library has managed to eliminate over the years.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With