py vs pyc

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

    #1

    py vs pyc

    Hi

    I want to know that is .pyc files execute faster than .py files,? since
    both are interpreted by python , is there any speedup in using one or
    other.?

    e.g.

    what is the difference between runing from cmd "python test.py" and
    "python test.pyc"


    thanks

  • Tomasz Lisowski

    #2
    Re: py vs pyc

    Sakcee wrote:[color=blue]
    > Hi
    >
    > I want to know that is .pyc files execute faster than .py files,? since
    > both are interpreted by python , is there any speedup in using one or
    > other.?
    >
    > e.g.
    >
    > what is the difference between runing from cmd "python test.py" and
    > "python test.pyc"
    >
    >
    > thanks
    >[/color]
    When running "python test.py" the interpreter will first precompile the
    test.py source file, and then execute it. When running "python
    test.pyc", the interpreter will go straight to the execution of the script.

    Tomasz

    Comment

    • mrmakent@cox.net

      #3
      Re: py vs pyc

      Sakcee wrote:
      [color=blue]
      > what is the difference between runing from cmd "python test.py" and
      > "python test.pyc"[/color]

      When you run 'python test.py', the python interpreter first looks to
      see if 'test.pyc' (which is the byte-code compiled version of
      'test.py') exists, and if it is more recent than 'test.py'. If so, the
      interpreter runs it. If it does not exist, or 'test.py' is more recent
      than it (meaning you have changed the source file), the interpreter
      first compiles 'test.py' to 'test.pyc'.

      So you can see that you don't need to directly run 'test.pyc'; the
      interpreter handles all that for you. Except for the very first time
      you run 'test.py' after creating or updating it, python will
      automactically run the pre-compiled version, the '.pyc'.

      There is one exception to all the above: If you put '#! /usr/bin/env
      python' on the first line of 'test.py', make it executable, and then
      run 'test.py' by itself (without explicitly invoking 'python', then
      'test.py' does not get compiled to 'test.pyc', meaning that the
      compilation step has to be done every time you run 'test.py' this way.

      Comment

      • Fredrik Lundh

        #4
        Re: py vs pyc

        mrmakent@cox.ne t wrote:
        [color=blue][color=green]
        >> what is the difference between runing from cmd "python test.py" and
        >> "python test.pyc"[/color]
        >
        > When you run 'python test.py', the python interpreter first looks to
        > see if 'test.pyc' (which is the byte-code compiled version of
        > 'test.py') exists, and if it is more recent than 'test.py'.[/color]

        nope.

        the import statement does this, but the Python interpreter doesn't (running
        test.py as a script isn't the same thing as importing it as a module). the inter-
        preter does recognize a compiled file, though:

        $ cat >q.py
        print "hello"
        $ python2.4 q.py
        hello
        $ ls q.pyc
        ls: q.pyc: No such file or directory
        $ python2.4 -c "import q"
        hello
        $ ls q.pyc
        q.pyc
        $ python2.4 q.pyc
        hello

        however, this only works if you're passing in a filename:

        $ python2.4 <q.py
        hello
        $ python2.4 <q.pyc
        sys:1: DeprecationWarn ing: Non-ASCII character '\xf2' in file <stdin> on line 1,
        but no encoding declared; see http://www.python.org/peps/pep-0263.html for
        details
        File "<stdin>", line 1
        m=
        ^
        SyntaxError: invalid syntax

        and it usually won't work if you use another Python version:

        $ python2.3 q.pyc
        RuntimeError: Bad magic number in .pyc file

        </F>



        Comment

        Working...