Compiled bytecode

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LutherRevisited

    #1

    Compiled bytecode

    This may be a dumb question, but are there any practical advantages of
    compiling a python application to *.pyo, or *.pyc? I haven't noticed any
    difference in the performance of text *.py or a bytecompiled file.
  • JZ

    #2
    Re: Compiled bytecode

    Dnia 29 Dec 2004 23:57:14 GMT, LutherRevisited napisa³(a):
    [color=blue]
    > I haven't noticed any difference in the performance of text *.py
    > or a bytecompiled file.[/color]

    Importing modules works faster.

    --
    JZ ICQ:6712522


    Comment

    • Martin v. Löwis

      #3
      Re: Compiled bytecode

      LutherRevisited wrote:[color=blue]
      > This may be a dumb question, but are there any practical advantages of
      > compiling a python application to *.pyo, or *.pyc? I haven't noticed any
      > difference in the performance of text *.py or a bytecompiled file.[/color]

      For a large application, the startup cost may be noticable, as it first
      compiles all files to bytecode. So it is a good idea to keep the
      bytecode files around.

      As to what difference -O makes: in 2.4, it only
      - sets __debug__ to be false, at compile time,
      - removes assert statements
      - removes __doc__ strings from the bytecode

      Regards,
      Martin

      Comment

      • Peter Hansen

        #4
        Re: Compiled bytecode

        LutherRevisited wrote:[color=blue]
        > This may be a dumb question, but are there any practical advantages of
        > compiling a python application to *.pyo, or *.pyc? I haven't noticed any
        > difference in the performance of text *.py or a bytecompiled file.[/color]

        The main script is generally not compiled, but all imported
        scripts are generally compiled automatically, the first time
        they are imported, and never again unless the source changes.

        The compilation process is also generally very fast, basically
        not noticable if you are dealing with only small and few scripts,
        such as perhaps your test cases.

        For this reason and others, I would say there's no real
        practical advantage *performance-wise* to compiling your
        application, and in fact since it happens automatically
        anyway, even worrying about it is wasting your time.

        Packagers such as py2exe, however, will generally compile
        the source to .pyc files and package that, which makes
        installation simpler etc., so there are certainly *some*
        practical advantages, just not performance-related ones
        (generally).

        -Peter

        Comment

        • Rocco Moretti

          #5
          Re: Compiled bytecode

          Peter Hansen wrote:
          [color=blue]
          > The main script is generally not compiled, but all imported
          > scripts are generally compiled automatically, the first time
          > they are imported, and never again unless the source changes.[/color]

          Someone please correct me if I'm wrong, but I'm under the impression
          that the main script *is* compiled, but the byte-code compiled file is
          kept in memory, and not written to disk.

          That's what makes this work:

          ------ file1.py -----

          import dis

          def f():
          a = 1
          print a

          dis.dis(f)

          ---------------------
          [color=blue]
          > python file1.py[/color]
          4 0 LOAD_CONST 1 (1)
          3 STORE_FAST 0 (a)

          5 6 LOAD_FAST 0 (a)
          9 PRINT_ITEM
          10 PRINT_NEWLINE
          11 LOAD_CONST 0 (None)
          14 RETURN_VALUE[color=blue]
          >[/color]
          ----------------------

          Comment

          • Peter Hansen

            #6
            Re: Compiled bytecode

            Rocco Moretti wrote:[color=blue]
            > Peter Hansen wrote:[color=green]
            >> The main script is generally not compiled, but all imported
            >> scripts are generally compiled automatically, the first time
            >> they are imported, and never again unless the source changes.[/color]
            >
            > Someone please correct me if I'm wrong, but I'm under the impression
            > that the main script *is* compiled, but the byte-code compiled file is
            > kept in memory, and not written to disk.[/color]

            Rocco, you're quite correct and I misspoke. I did of course
            mean "the main script's bytecode is not written to disk in
            a .pyc file".

            To the OP: one might interpret your question differently, now
            that I look again at your usage of "compiled". If you
            thought that code that didn't get into a .pyc file was somehow
            not compiled, you were wrong. *All* Python code gets compiled,
            whether it's in the main script, imported modules, or even
            in an exec statement or a call to eval(). The only difference
            is that imported modules have the output of the compilation
            process (i.e. the bytecode) cached in a .pyc or .pyo file
            to allow skipping a later redundant recompilation stage if
            the .py source has not changed. Therefore one could say that
            Python never executes .py files, so your observation about
            performance is correct: there is no difference.

            -Peter

            Comment

            Working...