Python编译成C语言,性能有多暴力?
我這里用的Python環(huán)境是Anaconda3 2019.7
這里測(cè)試的程序是找出所有1000以?xún)?nèi)的勾股數(shù)。
a∈[1, 1000],b∈[1, 1000], c∈[1, 1000]
滿(mǎn)足a2 + b2 = c2 有多少種解?
如果用普通的python去寫(xiě),代碼如下:
創(chuàng)建一個(gè)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()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
創(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
1
2
3
4
5
6
7
8
9
10
11
12
13
這時(shí)候還沒(méi)有編譯成C去運(yùn)行,只是從pyx文件導(dǎo)入函數(shù)去使用。
執(zhí)行結(jié)束以后,結(jié)果為881,耗時(shí)為57603毫秒,太慢了。
現(xiàn)在開(kāi)始,我們編譯成C語(yǔ)言去運(yùn)行,看一下效果。
修改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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
創(chuàng)建setup.py (這一步其實(shí)可以不做,因?yàn)檫@只是把編譯結(jié)果寫(xiě)入本地磁盤(pán),給我們展示生成的C語(yǔ)言代碼長(zhǎng)什么樣)
# 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")
)
1
2
3
4
5
6
7
8
9
10
11
12
13
依次在pycharm的終端執(zhí)行以下命令:
set PYTHONHOME=D:\Anaconda3
conda activate
python setup.py build_ext --inplace
1
2
3
這將生成.c文件和一些不知道什么文件。
執(zhí)行main.py以后,結(jié)果不變,實(shí)行時(shí)間由原來(lái)的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;
? ? }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
執(zhí)行時(shí)間是130ms左右。
————————————————
版權(quán)聲明:本文為CSDN博主「Xeon_CC」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/Xeon_CC/article/details/122582936
總結(jié)
以上是生活随笔為你收集整理的Python编译成C语言,性能有多暴力?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 广州永胜大道西附近有什么好吃的?
- 下一篇: 撞车了保险公司全赔吗