前言:
文章里用的Python环境是Anaconda3 2019.7
这里测试的程序是找出所有1000以内的勾股数。
a∈[1, 1000],b∈[1, 1000], c∈[1, 1000]
足a² + b² = c²
有多少种解?
如果用普通的python去写,代码如下:
创建一个main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# 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() |
创建pyth_triples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# 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文件导入函数去使用。
执行结束以后,结果为881,耗时为57603毫秒,太慢了。
现在开始,我们编译成C语言去运行,看一下效果。
修改pyth_triples.pyx
文件,定义的变量都改为cdef int xxx = 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# 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 |
创建setup.py (这一步其实可以不做,因为这只是把编译结果写入本地磁盘,给我们展示生成的C语言代码长什么样)
1
2
3
4
5
6
7
8
9
10
11
12
|
# 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的终端执行以下命令:
1
2
3
|
set PYTHONHOME = D:\Anaconda3 conda activate python setup.py build_ext - - inplace |
这将生成.c文件和一些不知道什么文件
执行main.py
以后,结果不变,实行时间由原来的57603毫秒减少到35毫秒左右,相差1600多倍。
如果用Java去跑这套代码
Java代码:
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
|
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; } } |
执行时间是130ms左右。
到此这篇关于如何将Python编译成C语言的文章就介绍到这了,更多相关将Python编译成C语言内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/Xeon_CC/article/details/122582936