Python LOC, .exe size, and refactoring

Discussion in 'Python' started by CM, Feb 22, 2012.

  1. CM

    CM Guest

    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).
     
    CM, Feb 22, 2012
    #1
    1. Advertising

  2. On Tue, 21 Feb 2012 19:51:07 -0800, CM wrote:

    > 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.


    --
    Steven
     
    Steven D'Aprano, Feb 22, 2012
    #2
    1. Advertising

  3. On Wed, Feb 22, 2012 at 4:29 PM, Steven D'Aprano
    <> wrote:
    > 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
     
    Chris Angelico, Feb 22, 2012
    #3
  4. CM

    CM Guest

    On Feb 22, 12:29 am, Steven D'Aprano <steve
    > wrote:
    > On Tue, 21 Feb 2012 19:51:07 -0800, CM wrote:
    > > 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...
    >
    > 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?
    >
    > > 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.
    >
    > --
    > Steven


    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
     
    CM, Feb 24, 2012
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marcelo Birnbach [MS]
    Replies:
    0
    Views:
    3,330
    Marcelo Birnbach [MS]
    Jun 28, 2003
  2. fpgawizz

    not able to write to addr loc x0

    fpgawizz, Apr 2, 2005, in forum: VHDL
    Replies:
    1
    Views:
    441
    info_
    Apr 2, 2005
  3. El Durango

    Eclipse question regarding LOC.

    El Durango, Jul 19, 2004, in forum: Java
    Replies:
    4
    Views:
    25,956
    James
    Jul 20, 2004
  4. Adriaan Renting

    LOC in Python and C++ in large projects

    Adriaan Renting, Jul 1, 2005, in forum: Python
    Replies:
    1
    Views:
    1,978
    Mike Meyer
    Jul 1, 2005
  5. Beyond LOC

    , Jan 15, 2007, in forum: C++
    Replies:
    5
    Views:
    375
    Ian Collins
    Jan 16, 2007
Loading...

Share This Page