網站首頁 編程語言 正文
前言:
文章里用的Python環境是Anaconda3 2019.7
這里測試的程序是找出所有1000以內的勾股數。
a∈[1, 1000],b∈[1, 1000], c∈[1, 1000]
足a2 + b2 = c2
有多少種解?
如果用普通的python去寫,代碼如下:
創建一個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()
創建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文件導入函數去使用。
執行結束以后,結果為881,耗時為57603毫秒,太慢了。
現在開始,我們編譯成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
創建setup.py (這一步其實可以不做,因為這只是把編譯結果寫入本地磁盤,給我們展示生成的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的終端執行以下命令:
set PYTHONHOME=D:\Anaconda3 conda activate python setup.py build_ext --inplace
這將生成.c文件和一些不知道什么文件
執行main.py
以后,結果不變,實行時間由原來的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; ? ? } }
執行時間是130ms左右。
原文鏈接:https://blog.csdn.net/Xeon_CC/article/details/122582936
相關推薦
- 2023-07-07 關于啟動報錯ModuleNotFoundError:No module named ‘python-
- 2022-10-16 實例詳解Python中的numpy.abs和abs函數_python
- 2022-09-15 Android自定義ViewGroup實現選擇面板_Android
- 2022-09-08 pandas刪除部分數據后重新生成索引的實現_python
- 2022-05-16 C語言中有哪些字符處理函數你知道嗎_C 語言
- 2022-10-16 Ant?Design?組件庫之步驟條實現_React
- 2023-03-23 Rust應用調用C語言動態庫的操作方法_Rust語言
- 2022-07-25 Oracle中的序列SEQUENCE詳解_oracle
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支