Python LOC, .exe size, and refactoring

C

CM

I have an application that I was hoping to reduce a bit the size of
its .exe when "packaged" with py2exe. I'm removing some Python
modules such as Tkinter, etc., but now wonder how much I could size I
could reduce by refactoring--and therefore shortening--my code.

Is there a rule of thumb that predicts the relationship between the
number of lines of Python code and the resultant size of the
application (leaving aside the size of imported modules)? Or is there
a way to roughly estimate how much would refactoring the code as much
as I reasonably can help? (For example, in some cases there is some
cut and paste coding...I know, it's bad).
 
S

Steven D'Aprano

I have an application that I was hoping to reduce a bit the size of its
.exe when "packaged" with py2exe. I'm removing some Python modules such
as Tkinter, etc., but now wonder how much I could size I could reduce by
refactoring--and therefore shortening--my code.

Well that will depend on how much you refactor it, but frankly, unless
your code is truly awful, this will be a micro-optimization. py2exe
bundles a Python runtime environment plus your files into a single exe
file. Typically the runtime environment will be somewhere around 11MB for
wxPython GUI apps (or 4MB with compression turned on, which will slow
your application down).

http://www.py2exe.org/index.cgi/SingleFileExecutable

The runtime environment for Oracle's Java environment starts at 7MB and
is typically 15MB, plus whatever libraries your own code produces. For
dot-net applications, the framework can be up to 60MB.

http://weblogs.java.net/blog/stanleyh/archive/2005/05/deployment_unde.html

http://www.hanselman.com/blog/SmallestDotNetOnTheSizeOfTheNETFramework.aspx


While I think 60MB for a basic calculator app is taking the piss, this is
2011 not 1987 and we don't have to support floppy disks any more. 11MB
for a GUI app is nothing to be worried about. That takes, what, 3 minutes
to download even on a 512 kbps link?

Is there a rule of thumb that predicts the relationship between the
number of lines of Python code and the resultant size of the application
(leaving aside the size of imported modules)?

Yes. To a close approximation, for most applications:

size of bundled application = (
size of Python runtime environment + size of libraries used
)

Your code is most likely insignificant compared to the others.

Or is there a way to
roughly estimate how much would refactoring the code as much as I
reasonably can help? (For example, in some cases there is some cut and
paste coding...I know, it's bad).

Look at it this way: take the .pyc file from your code. How big is it?
Say, it's 200K. That's a BIG file -- the decimal module in the standard
library is only 152K. Suppose you could cut it in half -- you would save
100K. Even if you could somehow cut it down to 1K, you've saved less than
200K. Do you care?

Refactoring your code is double-plus good for maintainability. You should
do it anyway. But don't do it to shrink an 11MB exe down to 10.8MB.
 
C

Chris Angelico

While I think 60MB for a basic calculator app is taking the piss, this is
2011 not 1987 and we don't have to support floppy disks any more. 11MB
for a GUI app is nothing to be worried about. That takes, what, 3 minutes
to download even on a 512 kbps link?

There are other reasons for wanting to keep executable size down,
though - low-memory VMs and such (of course, "low-memory" still means
orders of magnitude more than yesterday's top-end box - the first
computer I ever used, my dad bought the super-enormous 20MB hard disk
option). But when you're looking at shrinking
... an 11MB exe down to 10.8MB

then it's pretty moot.

ChrisA
 
C

CM

Well that will depend on how much you refactor it, but frankly, unless
your code is truly awful, this will be a micro-optimization. py2exe
bundles a Python runtime environment plus your files into a single exe
file. Typically the runtime environment will be somewhere around 11MB for
wxPython GUI apps (or 4MB with compression turned on, which will slow
your application down).

http://www.py2exe.org/index.cgi/SingleFileExecutable

The runtime environment for Oracle's Java environment starts at 7MB and
is typically 15MB, plus whatever libraries your own code produces. For
dot-net applications, the framework can be up to 60MB.

http://weblogs.java.net/blog/stanleyh/archive/2005/05/deployment_unde...

http://www.hanselman.com/blog/SmallestDotNetOnTheSizeOfTheNETFramewor...

While I think 60MB for a basic calculator app is taking the piss, this is
2011 not 1987 and we don't have to support floppy disks any more. 11MB
for a GUI app is nothing to be worried about. That takes, what, 3 minutes
to download even on a 512 kbps link?


Yes. To a close approximation, for most applications:

size of bundled application = (
    size of Python runtime environment + size of libraries used
    )

Your code is most likely insignificant compared to the others.


Look at it this way: take the .pyc file from your code. How big is it?
Say, it's 200K. That's a BIG file -- the decimal module in the standard
library is only 152K. Suppose you could cut it in half -- you would save
100K. Even if you could somehow cut it down to 1K, you've saved less than
200K. Do you care?

Refactoring your code is double-plus good for maintainability. You should
do it anyway. But don't do it to shrink an 11MB exe down to 10.8MB.

Thanks. All helpful advice. I'm coming in around 14 MB when you
count some libraries, image files, etc., and I think I can live with
that, considering I was able to reduce it from about 20 MB at one
point by some excluding.

Che
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top