如何將Python編譯成C語言
前言:
文章里用的Python環(huán)境是Anaconda3 2019.7
這里測試的程序是找出所有1000以內(nèi)的勾股數(shù)。
a∈[1, 1000],b∈[1, 1000], c∈[1, 1000]
足a² + b² = c²
有多少種解?
如果用普通的python去寫,代碼如下:
創(chuàng)建一個main.py
# encoding=utf-8 # cython: language_level=3 import time import pyximport pyximport.install() import pyth_triples def main(): ? ? start = time.time() ? ? result = pyth_triples.count_triples(1000) ? ? duration = time.time() - start ? ? print(result, duration * 1000, "ms") if __name__ == '__main__': ? ? main()
創(chuàng)建pyth_triples.py
# encoding=utf-8 # cython: language_level=3 def count_triples(limit): ? ? result = 0 ? ? for a in range(1, limit + 1): ? ? ? ? for b in range(a + 1, limit + 1): ? ? ? ? ? ? for c in range(b + 1, limit + 1): ? ? ? ? ? ? ? ? if c ** 2 > a ** 2 + b ** 2: ? ? ? ? ? ? ? ? ? ? break ? ? ? ? ? ? ? ? if c ** 2 == (a ** 2 + b ** 2): ? ? ? ? ? ? ? ? ? ? result += 1 ? ? return result
這時候還沒有編譯成C去運行,只是從pyx文件導入函數(shù)去使用。
執(zhí)行結(jié)束以后,結(jié)果為881,耗時為57603毫秒,太慢了。
現(xiàn)在開始,我們編譯成C語言去運行,看一下效果。
修改pyth_triples.pyx
文件,定義的變量都改為cdef int xxx = 0
# encoding=utf-8 # cython: language_level=3 def count_triples(limit): ? ? cdef int result = 0 ? ? cdef int a = 0 ? ? cdef int b = 0 ? ? cdef int c = 0 ? ? for a in range(1, limit + 1): ? ? ? ? for b in range(a + 1, limit + 1): ? ? ? ? ? ? for c in range(b + 1, limit + 1): ? ? ? ? ? ? ? ? if c ** 2 > a ** 2 + b ** 2: ? ? ? ? ? ? ? ? ? ? break ? ? ? ? ? ? ? ? if c ** 2 == (a ** 2 + b ** 2): ? ? ? ? ? ? ? ? ? ? result += 1 ? ? return result
創(chuàng)建setup.py (這一步其實可以不做,因為這只是把編譯結(jié)果寫入本地磁盤,給我們展示生成的C語言代碼長什么樣)
# encoding=utf-8 # cython: language_level=3 from distutils.core import setup from Cython.Build import cythonize # set PYTHONHOME=D:\Anaconda3 # conda activate # python setup.py build_ext --inplace setup( ? ? ext_modules=cythonize("pyth_triples.pyx") )
依次在pycharm的終端執(zhí)行以下命令:
set PYTHONHOME=D:\Anaconda3 conda activate python setup.py build_ext --inplace
這將生成.c文件和一些不知道什么文件
執(zhí)行main.py
以后,結(jié)果不變,實行時間由原來的57603毫秒減少到35毫秒左右,相差1600多倍。
如果用Java去跑這套代碼
Java代碼:
public class TriplesTest { ? ? public static void main(String[] args) { ? ? ? ? long startTime = System.currentTimeMillis(); ? ? ? ? System.out.println(count_triples(1000)); ? ? ? ? long endTime = System.currentTimeMillis(); ? ? ? ? System.out.println("run time:" + (endTime - startTime) + "ms"); ? ? } ? ? public static int count_triples(int limit) { ? ? ? ? int result = 0; ? ? ? ? for (int a = 1; a <= limit; a++) { ? ? ? ? ? ? for (int b = a + 1; b <= limit; b++) { ? ? ? ? ? ? ? ? for (int c = b + 1; c <= limit; c++) { ? ? ? ? ? ? ? ? ? ? if (Math.pow(c, 2) > Math.pow(a, 2) + Math.pow(b, 2)) { ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? if (Math.pow(c, 2) == Math.pow(a, 2) + Math.pow(b, 2)) { ? ? ? ? ? ? ? ? ? ? ? ? result += 1; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return result; ? ? } }
執(zhí)行時間是130ms左右。
到此這篇關(guān)于如何將Python編譯成C語言的文章就介紹到這了,更多相關(guān)將Python編譯成C語言內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python?matplotlib設(shè)置多子圖、子圖間距及外邊距的幾種方式
子圖是Matplotlib中強大的功能之一,使用函數(shù)您可以方便地創(chuàng)建多個子圖,并使用Axes對象繪制各種圖形,下面這篇文章主要給大家介紹了關(guān)于Python?matplotlib設(shè)置多子圖、子圖間距及外邊距的幾種方式,需要的朋友可以參考下2024-02-02Python多進程入門、分布式進程數(shù)據(jù)共享實例詳解
這篇文章主要介紹了Python多進程入門、分布式進程數(shù)據(jù)共享,結(jié)合實例形式詳細分析了Python進程操作技巧數(shù)據(jù)共享相關(guān)實現(xiàn)技巧與注意事項,需要的朋友可以參考下2019-06-06使用OpenCV對車道進行實時檢測的實現(xiàn)示例代碼
這篇文章主要介紹了使用OpenCV對車道進行實時檢測的實現(xiàn)示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06