Pycom: 跨平台Python编译器,可获得20-30倍于Python解释器的速度,比pypy还要快
Pycom: A Python Compiler - A Python compiler, down to native code, using C++' by Omar
GitHub - Omyyyy/pycom: A Python compiler, down to native code, using C++
What is Pycom?
Pycom is effectively a compiler for Python code, bringing it down to a native executable (20-30x the speed of Python interpretation) with C++ as 'intermediate representation'. It supports almost all Pythonic syntax along with a lot of the standard library and inbuilt functions. To see what is currently supported and not supported, check the 'Examples' section below. Why and when use Pycom?
Why and when use Pycom?
Python is slow. While many optimisations and new implementations of it have vastly improved its speed, generating native code that can run as a standalone executable from Python code has never really been done. As a result, no matter what, Python code has never hit levels of speed and portability that C/C++. Pycom aims to tackle this. Due to Pycom (currently) not supporting all Python features from all versions, you should only really use it if you want to run simple applications with nothing too crazy or pythonic going on (again, check 'Examples')
Due to Pycom (currenly) not supporting all Python features from all versions, you should only really use it if you want to run simple applications with nothing too crazy or pythonic going on (again, check 'Examples')
Examples
What Pycom supports and is good at:
High iteration loops:
for i in range(1, 1000001):
if i % 3 == 0:
print(i)
def is_prime(n):
if n == 1:
return 0
for i in range(2, n):
if n%i == 0:
return 0
return 1
def main():
total = 0
for i in range(1, 101):
total += is_prime(i)
print(total)
Speed benchmarks and comparisons
Benchmark | CPython | Pycom | Pycom with --fastmath | pypy |
---|---|---|---|---|
Multiples of 3 and 5 | 9.383s | 0.133s | 0.106s | 0.495s |
Primes | 17.127s | 4.441s | 3.994s | 4.577s |
Stack Operations | 8.857s | 2.132s | 1.992s | 3.113s |
(All of these can be found under ./benchmarks/)
Supported Features
- All 'turing complete' features of Python: if, else, for, while, etc.
- f'' strings
- Some in built functions
- Some math library functions
- List comprehensions
- Python-style arbitarily large intergers
Not supported yet
- Pythonic ways of writing certain blocks (one line if...else, etc.)
- Multi-line string literals
- A lot of libraries included in stdlib
- Classes
- Try, except and finally blocks
- Heterogeneous lists; lists with more than one data type in them