Program performance in order of module loading

Asked

Viewed 32 times

0

Someone would tell me if there is a relevant difference in the performance of a program (in this case, done in Python) when importing all modules to be used from the beginning of the code instead of importing them only at the time (line) that will be used?

2 answers

0

There is no relevant performance difference in this case.

There are some functional differences. For example, if the module to uses the module b and the module b uses the module to, When using imports at the beginning of the code, you will get an error due to cyclical imports. If you place such imports only at the time you use them, there will be no mistake.

0


In fact, there is no absolute answer, and it depends on the profile of its application. Most often it is best to import at the beginning of the module - even by a language style convention.

A Python module, once imported into a running program, is no longer read from disk or re-compiled.

However, the first time it is imported, there may be some delay in executing its code and initializing the internal states. In general, it is not interesting that a program, once it is running, is giving "soquinhos" - for example - when meeting a specific network request, in a code that has not yet run since the server went up, if the import is within the function, the first request can take a much longer time than the others.

So, most of the time, to avoid this effect, and also by a style convention, import is placed all at the beginning of a module. But, there are cases where just the opposite is desired - it is preferable that a module be initialized faster and a possible delay in the initialization of a dependency happens only at runtime when (and if) a specific section is necessary. An example of this case is the initialization of Python itself - it is very important that the time it takes for the language itself to be ready to start processing the code of the program it will run is as short as possible, then everything that is not strictly necessary for the language to work has its import postponed.

And finally, there are cases of circular dependency between modules, where yes, it is better to place an import within a function or method, so that it only occurs after the other module has already been 100% initialized. An example of an application structure in which this order is important is in Flask applications with Sqlalchemy, where the declaration of the data model classes itself depends on the flask application and the Sqlalchemy engine have already been initialized. In this case the modules containing these models cannot be imported before the code that makes this initialization.This last case has not to do with performance, but with the logical organization of the application.

  • What option (all at the beginning or according to execution step) would you recommend for a Learning machine algorithm?

  • All in the beginning, no doubt.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.