Preface:
The Python environment used in the article is Anaconda3 2019.7
The program tested here is to find all the collinear numbers up to 1000.
a∈[1, 1000],b∈[1, 1000], c∈[1, 1000]
excessivea² + b² = c²
How many solutions are there?
If you were to write it in regular python, the code would look like this:
Create a
# encoding=utf-8 # cython: language_level=3 import time import pyximport () import pyth_triples def main(): start = () result = pyth_triples.count_triples(1000) duration = () - start print(result, duration * 1000, "ms") if __name__ == '__main__': main()
Create 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
At this point it's not compiled into C to run, it's just importing functions from pyx files to use.
At the end of the execution, the result is 881 and it took 57603 milliseconds, which is too slow.
Now to start, let's compile to C to run it and see the results.
modificationspyth_triples.pyx
file, the defined variables are changed tocdef 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
establish(This step can actually be left out, as it just writes the compilation results to local disk and shows us what the generated C code looks like)
# encoding=utf-8 # cython: language_level=3 from import setup from import cythonize # set PYTHONHOME=D:\Anaconda3 # conda activate # python build_ext --inplace setup( ext_modules=cythonize("pyth_triples.pyx") )
Execute the following commands sequentially in pycharm's terminal:
set PYTHONHOME=D:\Anaconda3 conda activate python build_ext --inplace
This generates .c files and some who-knows-what files
fulfillmentLater, the results remain the same and the implementation time is reduced from 57,603 milliseconds to about 35 milliseconds, a difference of more than 1600 times.
If you run this code in Java
Java code:
public class TriplesTest { public static void main(String[] args) { long startTime = (); (count_triples(1000)); long endTime = (); ("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 ((c, 2) > (a, 2) + (b, 2)) { break; } if ((c, 2) == (a, 2) + (b, 2)) { result += 1; } } } } return result; } }
The execution time is about 130ms.
To this article on how to compile Python into C language is introduced to this article, more related to compile Python into C language content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!