When do you use byte compilation?

Asked 2 years ago, Updated 2 years ago, 26 views

I found that I could compile python code, so I compared the effective speed.

for i in range (0,1000000):
    print i

I'm comparing it with this code.

$time python main.py
python main.py 0.80s user 0.64s system 71% cpu 2.000 total

$ time python main.py
python main.pyc 0.79s user 0.64s system 68% cpu 2.074 total

As I heard it was compiled, I thought it would simply speed up like Java, but it didn't change.
Could you tell me when it works and when it is common to use it?

python

2022-09-30 19:54

3 Answers

Can Python be compiled into machine language like C and other languages?
http://docs.python.jp/2.7/faq/design.html?highlight=pyc#python-c

Internally, Python source code is always translated into byte code representations, which are executed by Python's virtual machines.To avoid the overhead of repeated parsing of rarely changed modules, this byte code is written to a file ending in ".pic" each time a module is parsed.When the corresponding .py file changes, it is parsed and translated again and the .pyc file is rewritten.

Once the .pyc file is loaded, there is no difference in performance, and the byte code read from the .pyc file is exactly the same as the byte code generated by the direct conversion.The only difference is that reading code from .pyc files is faster than parsing and translating .py files, so having a precompiled .pyc file improves the boot time of Python scripts.If necessary, the Lib/compileall.py module can generate the appropriate .pyc files for the given set of modules.


2022-09-30 19:54

Languages like python run by converting the source into a pseudo-language once called byte code.
Therefore, if you use a previously converted byte code, you can omit the time of the conversion part from that source.
So a very long source would be effective. (On the contrary, a short source may have little effect, and on the contrary, it may have the opposite effect of longer loading time.)

It may also be useful if you don't want others to read the source directly.


2022-09-30 19:54

*.pyc file is only a pre-syntax parsing part.
If you want compilation like "Java" you should try PyPy, the implementation of the (JIT) interpreter that you compile into machine language at runtime, or Numba, the library that compiles into LLVM on a functional basis.

http://pypy.org/
http://numba.pydata.org/

The following is a personal impression, but
PyPy was developed to replace generic, regular python commands, so pure python
The code works well, but we understand that full compatibility is not expected when extended with C.
Numba is based on NumPy, so it is useful when the numerical calculation itself is heavy, but it has a limited use.


2022-09-30 19:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.