密碼子是一個高性能的Python實現,它將編譯到本機機器代碼沒有任何運行時開銷。一條線程上的典型加速度的python的典型加速度為10-100倍或以上。密碼子的性能通常與C/C ++的性能相當(有時比)。與Python不同,密碼子支持本機多線程,這可能會導致更高的加速。
將密碼子視為Python重新構想,以提前彙編,從頭開始構建,考慮到最佳性能。
CPYTHON的倒入替換:密碼子不是CPYTHON的倒入替換。 Python的某些方面不適合靜態彙編 - 我們不支持密碼子。有多種方法可以通過其JIT裝飾器或Python擴展後端在較大的Python代碼庫中使用密碼子。密碼子還支持通過其Python互操作性調用任何Python模塊。另請參見文檔中的“與Python的差異” 。
新的語法和語言結構:我們盡量避免添加新的語法,關鍵字或其他語言功能。雖然密碼子確實在幾個地方添加了一些新的語法(例如表達並行性),但我們試圖使其盡可能熟悉和直觀。
Linux(X86_64)和MACOS(X86_64和ARM64)的預構建二進製文件可與每個版本一起使用。下載並安裝:
/bin/bash -c " $( curl -fsSL https://exaloop.io/install.sh ) "
或者您可以從源構建。
密碼子是一種兼容Python的語言,許多Python程序將在任何修改時使用:
def fib ( n ):
a , b = 0 , 1
while a < n :
print ( a , end = ' ' )
a , b = b , a + b
print ()
fib ( 1000 )
codon
編譯器具有許多選項和模式:
# compile and run the program
codon run fib.py
# 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
# compile and run the program with optimizations enabled
codon run -release fib.py
# 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
# compile to executable with optimizations enabled
codon build -release -exe fib.py
./fib
# 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
# compile to LLVM IR file with optimizations enabled
codon build -release -llvm fib.py
# outputs file fib.ll
有關更多選項和示例,請參見文檔。
您可以從密碼子導入並使用任何Python軟件包。例如:
from python import matplotlib . pyplot as plt
data = [ x ** 2 for x in range ( 10 )]
plt . plot ( data )
plt . show ()
(如文檔中所述,請記住將CODON_PYTHON
環境變量設置為CPYTHON共享庫。)
這個素數示例顯示了密碼子的OpenMP支持,並添加了一行。 @par
註釋告訴編譯器,在這種情況下,使用動態時間表,塊大小為100和16個線程,並並行化以下for
。
from sys import argv
def is_prime ( n ):
factors = 0
for i in range ( 2 , n ):
if n % i == 0 :
factors += 1
return factors == 0
limit = int ( argv [ 1 ])
total = 0
@ par ( schedule = 'dynamic' , chunk_size = 100 , num_threads = 16 )
for i in range ( 2 , limit ):
if is_prime ( i ):
total += 1
print ( total )
密碼子支持寫作和執行GPU內核。這是一個計算Mandelbrot集的示例:
import gpu
MAX = 1000 # maximum Mandelbrot iterations
N = 4096 # width and height of image
pixels = [ 0 for _ in range ( N * N )]
def scale ( x , a , b ):
return a + ( x / N ) * ( b - a )
@ gpu . kernel
def mandelbrot ( pixels ):
idx = ( gpu . block . x * gpu . block . dim . x ) + gpu . thread . x
i , j = divmod ( idx , N )
c = complex ( scale ( j , - 2.00 , 0.47 ), scale ( i , - 1.12 , 1.12 ))
z = 0j
iteration = 0
while abs ( z ) <= 2 and iteration < MAX :
z = z ** 2 + c
iteration += 1
pixels [ idx ] = int ( 255 * iteration / MAX )
mandelbrot ( pixels , grid = ( N * N ) // 1024 , block = 1024 )
GPU編程也可以使用@par(gpu=True)
的@par
語法完成。
請參閱docs.exaloop.io以獲取深入文檔。