I use it here Python Environment is Anaconda3 2019.7
The procedure tested here is to find out all 1000 Number of shares within .
a∈[1, 1000],b∈[1, 1000], c∈[1, 1000]
satisfy a² + b² = c² How many solutions are there ?

If you use ordinary python Go write , The code is as follows ：
Create a 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()
establish 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
It hasn't been compiled yet C Go run , Just from pyx File import function to use .
After execution , The result is 881, Time consuming 57603 millisecond , It's too slow .

Starting now , We compiled it into C Language to run , Look at the effect .
modify pyth_triples.pyx file , All defined variables are changed to 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
establish setup.py （ This step can not be done , Because it just writes the compilation results to the local disk , Show us the generated C What does the language code look like ）
# 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") )
In turn pycharm The following commands are executed by the terminal ：
set PYTHONHOME=D:\Anaconda3 conda activate python setup.py build_ext --inplace
This will generate .c Files and some don't know what files .

implement main.py in the future , The result remains unchanged , The implementation time is changed from the original 57603 Milliseconds reduced to 35 About milliseconds , differ 1600 Multiple times .

If used Java Run this code
Java code ：
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; } }
The execution time is 130ms about .

Technology
Daily Recommendation
views 1