Splitting up large python module impact on performance?

Chris Angelico rosuav at gmail.com
Tue Jun 12 20:44:31 EDT 2018


On Wed, Jun 13, 2018 at 10:32 AM, Cameron Simpson <cs at cskk.id.au> wrote:
> I think I'm saying: don't worry unless your applications are very time
> critical (eg invoked very frequently and/or doing almost nothing after the
> "import" phase) or you notice a significant slowdown after your changes. And
> it is usually easier to stick things back together than it was to pull them
> apart if that happens.

^^ This.

You may find some speed-ups from loading only the parts you want. You
may find some slowdowns from extra levels of indirection. But more
than likely, neither matters. Design your code to be logical. If
splitting the files up lets you more logically organize your code, do
it; if it's arbitrary (piece1, piece2, piece3), don't.

A general rule for refactoring: For any piece of code you could
describe, you should be able to say instantly which file it belongs
in. That way, when you notice a behavioural bug, you go "oh that's in
class X, so I need to edit file Y". If you can't do that, refactoring
your code may help.

There are no arbitrary file size limits in Python. Four thousand lines
of code is fine if they logically go together; you could go as far as
ten thousand or even more, and it won't be a problem. Split things up
exactly as far as they logically need to be split, and no further.

ChrisA



More information about the Python-list mailing list