Splitting up large python module impact on performance?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Jun 12 22:28:06 EDT 2018


On Tue, 12 Jun 2018 15:00:44 -0700, Bill Deegan wrote:

> Greetings,
> 
> I'm doing some refactoring on a fairly large python codebase. Some of
> the files are > 4000 lines long and contain many classes.
> 
> Should I expect any performance hit from splitting some of the classes
> out to other files?

Depends on whether you split it out to 4000 one-line files or 10 100 line 
files.

But in practice, no, there will be no runtime performance hit aside from 
the very small cost of needing to import X files instead of a single 
file. The actual performance of the classes themselves should be 
unchanged.

However, you'll need to be careful to manage circular dependencies, where 
module A depends on B which depends on C which depends on A. (Linear 
dependencies are fine: A depends on B which depends on C, which depends 
on nothing.) Circular dependencies can be managed in various ways, but 
the best way to manage them is to avoid them altogether.

In other words: if your classes are very tightly coupled, you might have 
a hard time splitting them into multiple files. If they are loosely 
coupled, you shouldn't have any trouble at all.


-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson




More information about the Python-list mailing list