{"id":2815,"date":"2024-03-23T00:43:01","date_gmt":"2024-03-22T16:43:01","guid":{"rendered":"http:\/\/www.ccwifi.cc\/blogs\/?p=2815"},"modified":"2024-03-23T00:43:01","modified_gmt":"2024-03-22T16:43:01","slug":"30%e5%80%8d%e4%bd%bf%e7%94%a8cython%e5%8a%a0%e9%80%9fpython2106","status":"publish","type":"post","link":"https:\/\/www.ccwifi.cc\/blogs\/2024\/03\/23\/30%e5%80%8d%e4%bd%bf%e7%94%a8cython%e5%8a%a0%e9%80%9fpython2106\/","title":{"rendered":"30\u500d\uff01\u4f7f\u7528Cython\u52a0\u901fPython\u4ee3\u7801"},"content":{"rendered":"<div>\n<p>\u5728\u7ebfwifi\u8dd1\u5305 \u91d1\u521a\u5305\u8dd1\u5305 cap\u8dd1\u5305 hccapx ewsa\u5728\u7ebf \u5c31\u6765 <strong><a href=\"https:\/\/ccwifi.cc\" target=\"_blank\" rel=\"noopener\">\u63e1\u624b\u5305\u8dd1\u5305<\/a><\/strong><\/p>\n<p>\u5404\u4f4d\u597d \u53c8\u89c1\u9762\u4e86 \u6211\u662f\u66f9\u64cd \u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\u65b0\u7684\u6559\u7a0b<\/p>\n<p>\u5e0c\u671b\u5404\u4f4d\u7ec6\u5fc3\u5b66\u4e60 \u4f4e\u8c03\u7528\u7f51<\/p>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/www.ccwifi.cc\/blogs\/wp-content\/uploads\/2024\/03\/1711125778406_0.jpg\" alt=\"\u4e13\u4e1a\u8dd1\u5305\u5b57\u5178\u4e0b\u8f7d\"><img decoding=\"async\" src=\"http:\/\/www.ccwifi.cc\/blogs\/wp-content\/uploads\/2024\/03\/1711125778406_1.png\" alt=\"\u4e13\u4e1a\u8dd1\u5305\u5b57\u5178\u4e0b\u8f7d\"><\/p>\n<p>\u6807\u661f\u2605\u516c\u4f17\u53f7  \u7231\u4f60\u4eec\u2665  \u4f5c\u8005\uff1aGeorge Seif\u3001Thomas Wolf\u3001Lukas Frei  \u7f16\u8bd1\uff1a1+1=6 | \u516c\u4f17\u53f7\u6d77\u5916\u90e8  \u8fd1\u671f\u539f\u521b\u6587\u7ae0\uff1a  \u2665\u2665\u26652\u4e07\u5b57\u5e72\u8d27\uff1a  \u2665\u2665\u2665\u2665\u2665\u2665\u2665\u2665\u2665\u2665\u2665\u2665\u2665\u2665  \u524d\u8a00  \u60a8\u53ef\u80fd\u7ecf\u5e38\u542c\u5230\u6709\u5173Python\u8fd0\u884c\u901f\u5ea6\u6162\u7684\u62b1\u6028\u3002\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u76f8\u6bd4\uff0cPython\u786e\u5b9e\u8f83\u6162\u3002\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u52a0\u901f\u4ee3\u7801\uff1a\u5982\u679c\u60a8\u7684\u4ee3\u7801\u662f\u7eafPython\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u5927\u578b\u7684for\u5faa\u73af\uff0c\u60a8\u53ea\u80fd\u4f7f\u7528\u5b83\uff0c\u800c\u4e0d\u80fd\u4f7f\u7528\u77e9\u9635\uff0c\u56e0\u4e3a\u6570\u636e\u5fc5\u987b\u6309\u987a\u5e8f\u5904\u7406\u3002\u90a3\u4e48\u6709\u6ca1\u6709\u529e\u6cd5\u52a0\u5febPython\u672c\u8eab\u7684\u901f\u5ea6\u5462\uff1f\u8ba9\u6211\u4eec\u6765\u770b\u770bCython\u5427\uff01<\/p>\n<pre style=\"margin:0;padding:0;border-radius:none;background:none\"><code style=\"border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;padding: 5.95px\" class=\"hljs-sublime\"><span style=\"font-size: 14px\">x = <span class=\"hljs-sublime-number\" style=\"width: 22px;font-weight: 400;font-style: normal\">0.5<\/span><\/span><span class=\"hljs-sublime-number\" style=\"width: 22px;font-weight: 400;font-style: normal\"><\/span><\/code><\/pre>\n<p>\u6587\u672b\u4e0b\u8f7dCython\u76f8\u5173\u4e66\u7c4d  \u4ec0\u4e48\u662fCython\uff1f  Cython\u662fPython\u548cC \/ C++\u4e4b\u95f4\u7684\u4e00\u4e2a\u4e2d\u95f4\u6b65\u9aa4\u3002\u5b83\u5141\u8bb8\u60a8\u7f16\u5199\u7eafPython\u4ee3\u7801\uff0c\u5e76\u8fdb\u884c\u4e00\u4e9b\u5c0f\u7684\u4fee\u6539\uff0c\u7136\u540e\u5c06\u5176\u76f4\u63a5\u7ffb\u8bd1\u6210C\u4ee3\u7801\u3002Cython\u8bed\u8a00\u662fPython\u7684\u4e00\u4e2a\u8d85\u96c6\uff0c\u5b83\u5305\u542b\u4e24\u79cd\u7c7b\u578b\u7684\u5bf9\u8c61\uff1a\u60a8\u53ea\u9700\u5411\u6bcf\u4e2a\u53d8\u91cf\u6dfb\u52a0\u7c7b\u578b\u4fe1\u606f\u3002\u901a\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre style=\"margin:0;padding:0;border-radius:none;background:none\"><code style=\"border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;padding: 5.95px\" class=\"hljs-sublime\"><span style=\"font-size: 14px\">cdef float x = <span class=\"hljs-sublime-number\" style=\"width: 22px;font-weight: 400;font-style: normal\">0.5<\/span><\/span><span class=\"hljs-sublime-number\" style=\"width: 22px;font-weight: 400;font-style: normal\"><\/span><\/code><\/pre>\n<pre style=\"margin:0;padding:0;border-radius:none;background:none\"><code style=\"border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;padding: 5.95px\" class=\"hljs-sublime\"><span style=\"font-size: 14px\">pip install cython<\/span><\/code><\/pre>\n<p>\u4f7f\u7528Cython\uff0c\u6211\u4eec\u4e3a\u8be5\u53d8\u91cf\u6dfb\u52a0\u4e86\u4e00\u4e2a\u7c7b\u578b\u58f0\u660e\uff1a<\/p>\n<p>\u8fd9\u544a\u8bc9Cython\uff0c\u8be5\u53d8\u91cf\u662f\u4e00\u4e2a\u6d6e\u70b9\u6570\uff0c\u5c31\u50cf\u6211\u4eec\u5728C\u4e2d\u6240\u505a\u7684\u4e00\u6837\u3002\u5bf9\u4e8e\u7eafPython\uff0c\u53d8\u91cf\u7684\u7c7b\u578b\u662f\u52a8\u6001\u786e\u5b9a\u7684\u3002Cython\u4e2d\u7684\u663e\u5f0f\u7c7b\u578b\u58f0\u660e\u4f7f\u5176\u8f6c\u6362\u4e3aC\u4ee3\u7801\u6210\u4e3a\u53ef\u80fd\uff0c\u56e0\u4e3a\u663e\u5f0f\u7c7b\u578b\u58f0\u660e\u9700\u8981+\u3002\u6709\u8bb8\u591a\u65b9\u6cd5\u53ef\u4ee5\u6d4b\u8bd5\u3001\u7f16\u8bd1\u548c\u53d1\u5e03Cython\u4ee3\u7801\u3002Cython\u751a\u81f3\u53ef\u4ee5\u50cfPython\u4e00\u6837\u76f4\u63a5\u5728Jupyter Notebook\u4e2d\u4f7f\u7528\u3002\u5b89\u88c5Cython\u53ea\u9700\u8981\u4e00\u884cpip\uff1a<\/p>\n<pre style=\"margin:0;padding:0;border-radius:none;background:none\"><code style=\"border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;padding: 6px\" class=\"hljs-sublime\"><span style=\"font-size: 14px\">%load_ext Cython<\/span><\/code><\/pre>\n<p>\u4f7f\u7528Cython\u9700\u8981\u5b89\u88c5C\u8bed\u8a00\u7f16\u8bd1\u5668\uff0c\u56e0\u6b64\uff0c\u5b89\u88c5\u8fc7\u7a0b\u56e0\u60a8\u5f53\u524d\u7684\u64cd\u4f5c\u7cfb\u7edf\u800c\u5f02\u3002\u5bf9\u4e8eLinux\uff0c\u901a\u5e38\u4f7f\u7528GNU C\u7f16\u8bd1\u5668\uff08gncc\uff09\u3002\u5bf9\u4e8eMac OS\uff0c\u60a8\u53ef\u4ee5\u4e0b\u8f7dXcode\u4ee5\u83b7\u53d6gncc\u3002\u800c\u5728Windows\u684c\u9762\u7cfb\u7edf\u4e0b\u5b89\u88c5C\u7f16\u8bd1\u5668\u4f1a\u66f4\u590d\u6742\u3002\u4f7f\u7528%load_ext Cython\u6307\u4ee4\u5728Jupyter notebook\u4e2d\u52a0\u8f7dCython\u6269\u5c55\u3002\u7136\u540e\u901a\u8fc7\u6307\u4ee4%%cython\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u50cfPython\u4e00\u6837\u5728Jupyter notebook\u4e2d\u4f7f\u7528Cython\u3002\u5982\u679c\u5728\u6267\u884cCython\u4ee3\u7801\u65f6\u9047\u5230\u7f16\u8bd1\u9519\u8bef\uff0c\u8bf7\u68c0\u67e5Jupyter\u7ec8\u7aef\u7684\u5b8c\u6574\u8f93\u51fa\u4fe1\u606f\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u53ef\u80fd\u662f\u56e0\u4e3a\u5728%%cython\u4e4b\u540e\u9057\u6f0f\u4e86-+\u6807\u7b7e\uff08\u6bd4\u5982\u5f53\u60a8\u4f7f\u7528spaCy Cython\u63a5\u53e3\u65f6\uff09\u3002\u5982\u679c\u7f16\u8bd1\u5668\u62a5\u544a\u6709\u5173NumPy\u7684\u9519\u8bef\uff0c\u5219\u53ef\u80fd\u662f\u9057\u6f0f\u4e86import numpy\u3002\u5982\u679c\u60a8\u8981\u5728IPython\u4e2d\u4f7f\u7528Cython\uff0c\u8bf7\u9996\u5148\u4ecb\u7ecd\u4e00\u4e0bIPython Magic\u547d\u4ee4\u3002Magic\u547d\u4ee4\u4ee5\u767e\u5206\u53f7\u5f00\u5934\uff0c\u901a\u5e38\u6709\u4e24\u79cd\u7c7b\u578b\uff1a\u9996\u5148\u8fd0\u884c\u4ee5\u4e0b\u8bed\u53e5\u5f15\u5165Cython\uff1a<\/p>\n<pre style=\"margin:0;padding:0;border-radius:none;background:none\"><code style=\"border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;padding: 6px\" class=\"hljs-sublime\"><span style=\"font-size: 14px\">%%cython<\/span><\/code><\/pre>\n<pre style=\"margin:0;padding:0;border-radius:none;background:none\"><code style=\"border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;padding: 5.95px\" class=\"hljs-sublime\"><span style=\"width: 86px;font-weight: 400;font-style: normal;font-size: 14px\"><span class=\"hljs-sublime-keyword\" style=\"width: 22px;font-weight: 400;font-style: normal\">def<\/span> <span class=\"hljs-sublime-title\" style=\"width: 29px;font-weight: 400;font-style: normal\">test<\/span><span class=\"hljs-sublime-params\" style=\"width: 22px;font-weight: 400;font-style: normal\">(x)<\/span>:<\/span><br> <span style=\"font-size: 14px\">&nbsp;&nbsp;&nbsp;&nbsp;y = <span class=\"hljs-sublime-number\" style=\"font-size: 14px;width: 7px;font-weight: 400;font-style: normal\">1<\/span><\/span><br> <span style=\"font-size: 14px\"><span class=\"hljs-sublime-keyword\" style=\"font-size: 14px;width: 22px;font-weight: 400;font-style: normal\">&nbsp;&nbsp;&nbsp;&nbsp;for<\/span> i <span class=\"hljs-sublime-keyword\" style=\"font-size: 14px;width: 14px;font-weight: 400;font-style: normal\">in<\/span> range(x+<span class=\"hljs-sublime-number\" style=\"font-size: 14px;width: 7px;font-weight: 400;font-style: normal\">1<\/span>):<\/span><br><span style=\"font-size: 14px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y *= i<\/span><br> <span style=\"font-size: 14px\"><span class=\"hljs-sublime-keyword\" style=\"font-size: 14px;width: 43px;font-weight: 400;font-style: normal\">&nbsp;&nbsp;&nbsp;&nbsp;return<\/span> y<\/span><\/code><\/pre>\n<p>\u7136\u540e\uff0c\u5728\u8fd0\u884cCython\u4ee3\u7801\u65f6\uff0c\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u4ee5\u4e0bCython\u4ee3\u7801\uff1a<\/p>\n<p>\u7136\u540e\uff0c\u60a8\u5c31\u53ef\u4ee5\u6109\u5feb\u5730\u4f7f\u7528Cython\u4e86\u3002Cython\u4e2d\u7684\u7c7b\u578b  \u5728Cython\u4e2d\uff0c\u53d8\u91cf\u548c\u51fd\u6570\u6709\u4e24\u7ec4\u4e0d\u540c\u7684\u7c7b\u578b\u3002\u5bf9\u4e8e\u53d8\u91cf\uff0c\u6211\u4eec\u6709\uff1a\u8bf7\u6ce8\u610f\uff0c\u6240\u6709\u8fd9\u4e9b\u7c7b\u578b\u90fd\u6765\u81eaC \/ C++\uff01\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u7ed3\u679c\u4f20\u9012\u7ed9C\u4ee3\u7801\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\uff0cCython\u4f1a\u81ea\u52a8\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u4e86\u89e3\u4e86Cython\u7c7b\u578b\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u76f4\u63a5\u5b9e\u73b0\u52a0\u901f\u4e86\uff01\u5982\u4f55\u4f7f\u7528Cython\u52a0\u901f\u4ee3\u7801  \u6211\u4eec\u9996\u5148\u9700\u8981\u8bbe\u7f6ePython\u4ee3\u7801\u7684\u57fa\u51c6\uff1a\u4e00\u4e2a\u7528\u4e8e\u8ba1\u7b97\u9636\u4e58\u7684for\u5faa\u73af\u3002\u539f\u59cb\u7684Python\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre style=\"margin:0;padding:0;border-radius:none;background:none\"><code style=\"border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;padding: 6px\" class=\"hljs-sublime\"><span style=\"font-size: 14px\"><span class=\"hljs-sublime-keyword\" style=\"width: 43px;font-weight: 400;font-style: normal\">import<\/span> pyximport; pyximport.install()<\/span><br><span style=\"font-size: 14px\"><span class=\"hljs-sublime-keyword\" style=\"font-size: 14px;width: 43px;font-weight: 400;font-style: normal\">import<\/span> my_cython_module<\/span><\/code><\/pre>\n<p>Cython\u7684\u5b9e\u73b0\u8fc7\u7a0b\u770b\u8d77\u6765\u975e\u5e38\u76f8\u4f3c\u3002\u9996\u5148\uff0c\u786e\u4fddCython\u4ee3\u7801\u6587\u4ef6\u5177\u6709.pyx\u6269\u5c55\u540d\u3002\u8fd9\u4e9b\u6587\u4ef6\u5c06\u7531Cython\u7f16\u8bd1\u5668\u7f16\u8bd1\u4e3aC\u6216C++\u6587\u4ef6\uff0c\u7136\u540e\u7531C\u7f16\u8bd1\u5668\u8fdb\u4e00\u6b65\u7f16\u8bd1\u4e3a\u5b57\u8282\u7801\u6587\u4ef6\u3002\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528pyximport\u5c06.pyx\u6587\u4ef6\u76f4\u63a5\u52a0\u8f7d\u5230Python\u7a0b\u5e8f\u4e2d\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.ccwifi.cc\/blogs\/wp-content\/uploads\/2024\/03\/1711125778406_2.jpg\" alt=\"\u4e13\u4e1a\u8dd1\u5305\u5b57\u5178\u4e0b\u8f7d\"><\/p>\n<p>\u60a8\u8fd8\u53ef\u4ee5\u5c06\u81ea\u5df1\u7684Cython\u4ee3\u7801\u6784\u5efa\u4e3aPython\u5305\uff0c\u7136\u540e\u50cf\u6b63\u5e38\u7684Python\u5305\u4e00\u6837\u5bfc\u5165\u6216\u53d1\u5e03\u3002\u4f46\u662f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u9700\u8981\u66f4\u591a\u7684\u65f6\u95f4\uff0c\u7279\u522b\u662f\u5982\u679c\u60a8\u5e0c\u671bCython\u5305\u80fd\u591f\u5728\u6240\u6709\u5e73\u53f0\u4e0a\u8fd0\u884c\u3002\u5982\u679c\u60a8\u9700\u8981\u4e00\u4e2a\u53c2\u8003\u793a\u4f8b\uff0c\u53ef\u4ee5\u67e5\u770bspaCy\u7684\u5b89\u88c5\u811a\u672c\uff1a<\/p>\n<pre style=\"margin:0;padding:0;border-radius:none;background:none\"><code style=\"border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;padding: 5.95px\" class=\"hljs-sublime\"><span style=\"font-size: 14px\">cpdef int test(int x):<\/span><br> <span style=\"font-size: 14px\">&nbsp;&nbsp;&nbsp;&nbsp;cdef int y = <span class=\"hljs-sublime-number\" style=\"font-size: 14px;width: 7px;font-weight: 400;font-style: normal\">1<\/span><\/span><br> <span style=\"font-size: 14px\">&nbsp;&nbsp;&nbsp;&nbsp;cdef int i<\/span><br> <span style=\"font-size: 14px\"><span class=\"hljs-sublime-keyword\" style=\"font-size: 14px;width: 22px;font-weight: 400;font-style: normal\">&nbsp;&nbsp;&nbsp;&nbsp;for<\/span> i <span class=\"hljs-sublime-keyword\" style=\"font-size: 14px;width: 14px;font-weight: 400;font-style: normal\">in<\/span> range(x+<span class=\"hljs-sublime-number\" style=\"font-size: 14px;width: 7px;font-weight: 400;font-style: normal\">1<\/span>):<\/span><br> <span style=\"font-size: 14px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y *= i<\/span><br> <span style=\"font-size: 14px\"><span class=\"hljs-sublime-keyword\" style=\"font-size: 14px;width: 43px;font-weight: 400;font-style: normal\">&nbsp;&nbsp;&nbsp;&nbsp;return<\/span> y<\/span><\/code><\/pre>\n<pre style=\"margin:0;padding:0;border-radius:none;background:none\"><code style=\"border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;padding: 5.95px\" class=\"hljs-sublime\"><span style=\"font-size: 14px\"><span class=\"hljs-sublime-keyword\" style=\"width: 28px;font-weight: 400;font-style: normal\">from<\/span> distutils.core <span class=\"hljs-sublime-keyword\" style=\"width: 42px;font-weight: 400;font-style: normal\">import<\/span> setup<\/span><br><span style=\"font-size: 14px\"><span class=\"hljs-sublime-keyword\" style=\"font-size: 14px;width: 28px;font-weight: 400;font-style: normal\">from<\/span> Cython.Build <span class=\"hljs-sublime-keyword\" style=\"font-size: 14px;width: 42px;font-weight: 400;font-style: normal\">import<\/span> cythonize<\/span><br><br><span style=\"font-size: 14px\">setup(ext_modules = cythonize(<span class=\"hljs-sublime-string\" style=\"font-size: 14px;width: 114px;font-weight: 400;font-style: normal\">'run_cython.pyx'<\/span>))<\/span><\/code><\/pre>\n<p>\u6700\u7ec8\uff0cPython\u89e3\u91ca\u5668\u5c06\u80fd\u591f\u8c03\u7528\u8fd9\u4e9b\u5b57\u8282\u7801\u6587\u4ef6\u3002\u5bf9\u4ee3\u7801\u672c\u8eab\u7684\u552f\u4e00\u66f4\u6539\u662f\u6211\u4eec\u5df2\u7ecf\u4e3a\u6bcf\u4e2a\u53d8\u91cf\u548c\u51fd\u6570\u58f0\u660e\u4e86\u7c7b\u578b\u3002\u8bf7\u6ce8\u610f\uff0c\u51fd\u6570\u5177\u6709cpdef\u4ee5\u786e\u4fdd\u6211\u4eec\u53ef\u4ee5\u4ecePython\u8c03\u7528\u5b83\u3002\u53e6\u5916\uff0c\u8bf7\u6ce8\u610f\u6211\u4eec\u7684\u5faa\u73af\u53d8\u91cfi\u5982\u4f55\u5177\u6709\u7c7b\u578b\u3002\u60a8\u9700\u8981\u4e3a\u51fd\u6570\u4e2d\u7684\u6240\u6709\u53d8\u91cf\u8bbe\u7f6e\u7c7b\u578b\uff0c\u4ee5\u4fbfC\u7f16\u8bd1\u5668\u77e5\u9053\u4f7f\u7528\u54ea\u79cd\u7c7b\u578b\uff01\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2asetup.py\u6587\u4ef6\uff0c\u5c06Cython\u4ee3\u7801\u7f16\u8bd1\u4e3aC\u4ee3\u7801\uff1a<\/p>\n<pre style=\"margin:0;padding:0;border-radius:none;background:none\"><code style=\"border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;padding: 5.95px\" class=\"hljs-sublime\"><span style=\"font-size: 14px\">python setup.py build_ext --inplace<\/span><\/code><\/pre>\n<pre style=\"margin:0;padding:0;border-radius:none;background:none\"><code style=\"border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;padding: 5.95px\" class=\"hljs-sublime\"><span style=\"font-size: 14px\"><span class=\"hljs-sublime-keyword\" style=\"width: 42px;font-weight: 400;font-style: normal\">import<\/span> run_python<\/span><br><span style=\"font-size: 14px\"><span class=\"hljs-sublime-keyword\" style=\"font-size: 14px;width: 42px;font-weight: 400;font-style: normal\">import<\/span> run_cython<\/span><br><span style=\"font-size: 14px\"><span class=\"hljs-sublime-keyword\" style=\"font-size: 14px;width: 42px;font-weight: 400;font-style: normal\">import<\/span> time<\/span><br><br><span style=\"font-size: 14px\">number = <span class=\"hljs-sublime-number\" style=\"font-size: 14px;width: 14px;font-weight: 400;font-style: normal\">10<\/span><\/span><br><br><span style=\"font-size: 14px\">start = time.time()<\/span><br><span style=\"font-size: 14px\">run_python.test(number)<\/span><br><span style=\"font-size: 14px\">end = time.time()<\/span><br><br><span style=\"font-size: 14px\">py_time = end - start<\/span><br><span style=\"font-size: 14px\">print(<span class=\"hljs-sublime-string\" style=\"font-size: 14px;width: 130px;font-weight: 400;font-style: normal\">\"Python time = {}\"<\/span>.format(py_time))<\/span><br><br><span style=\"font-size: 14px\">start = time.time()<\/span><br><span style=\"font-size: 14px\">run_cython.test(number)<\/span><br><span style=\"font-size: 14px\">end = time.time()<\/span><br><br><span style=\"font-size: 14px\">cy_time = end - start<\/span><br><span style=\"font-size: 14px\">print(<span class=\"hljs-sublime-string\" style=\"font-size: 14px;width: 130px;font-weight: 400;font-style: normal\">\"Cython time = {}\"<\/span>.format(cy_time))<\/span><br><br><span style=\"font-size: 14px\">print(<span class=\"hljs-sublime-string\" style=\"font-size: 14px;width: 101px;font-weight: 400;font-style: normal\">\"Speedup = {}\"<\/span>.format(py_time \/ cy_time))<\/span><\/code><\/pre>\n<p>\u7136\u540e\u6267\u884c\u7f16\u8bd1\uff1aBoom\uff01\u6211\u4eec\u7684C\u4ee3\u7801\u5df2\u7ecf\u7f16\u8bd1\u597d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e86\uff01\u60a8\u5c06\u770b\u5230\uff0c\u5728Cython\u4ee3\u7801\u6240\u5728\u7684\u6587\u4ef6\u5939\u4e2d\uff0c\u6709\u6240\u6709\u8fd0\u884cC\u4ee3\u7801\u6240\u9700\u7684\u6587\u4ef6\uff0c\u5305\u62ecrun_cython.c\u6587\u4ef6\u3002\u5982\u679c\u60a8\u611f\u5174\u8da3\uff0c\u53ef\u4ee5\u67e5\u770bCython\u751f\u6210\u7684C\u4ee3\u7801\uff01\u73b0\u5728\u6211\u4eec\u51c6\u5907\u6d4b\u8bd5\u65b0\u7684C\u4ee3\u7801\uff01\u67e5\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff0c\u5b83\u5c06\u6267\u884c\u4e00\u4e2a\u901f\u5ea6\u6d4b\u8bd5\uff0c\u5c06\u539f\u59cbPython\u4ee3\u7801\u4e0eCython\u4ee3\u7801\u8fdb\u884c\u6bd4\u8f83\u3002\u73b0\u5728\u6211\u4eec\u51c6\u5907\u6d4b\u8bd5\u6211\u4eec\u65b0\u7684\u8d85\u5feb\u901fC\u4ee3\u7801\u4e86\uff01\u67e5\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff0c\u5b83\u6267\u884c\u901f\u5ea6\u6d4b\u8bd5\u4ee5\u5c06\u539f\u59cbPython\u4ee3\u7801\u4e0eCython\u4ee3\u7801\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.ccwifi.cc\/blogs\/wp-content\/uploads\/2024\/03\/1711125778406_3.jpg\" alt=\"\u4e13\u4e1a\u8dd1\u5305\u5b57\u5178\u4e0b\u8f7d\"><img decoding=\"async\" src=\"http:\/\/www.ccwifi.cc\/blogs\/wp-content\/uploads\/2024\/03\/1711125778406_4.jpg\" alt=\"\u4e13\u4e1a\u8dd1\u5305\u5b57\u5178\u4e0b\u8f7d\"><\/p>\n<p>Cython\u53ef\u4ee5\u8ba9\u60a8\u5728\u51e0\u4e4e\u6240\u6709\u539f\u59cbPython\u4ee3\u7801\u4e0a\u83b7\u5f97\u826f\u597d\u7684\u52a0\u901f\uff0c\u800c\u4e0d\u9700\u8981\u592a\u591a\u989d\u5916\u7684\u5de5\u4f5c\u3002\u9700\u8981\u6ce8\u610f\u7684\u5173\u952e\u662f\uff0c\u5faa\u73af\u6b21\u6570\u8d8a\u591a\uff0c\u5904\u7406\u7684\u6570\u636e\u8d8a\u591a\uff0cCython\u53ef\u4ee5\u63d0\u4f9b\u7684\u5e2e\u52a9\u5c31\u8d8a\u591a\u3002\u67e5\u770b\u4e0b\u8868\uff0c\u8be5\u8868\u663e\u793a\u4e86Cython\u4e3a\u4e0d\u540c\u9636\u4e58\u503c\u63d0\u4f9b\u7684\u901f\u5ea6\uff0c\u6211\u4eec\u4f7f\u7528Cython\u83b7\u5f97\u4e86\u8d85\u8fc736\u500d\u7684\u52a0\u901f\uff01<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.ccwifi.cc\/blogs\/wp-content\/uploads\/2024\/03\/1711125778406_5.png\" alt=\"\u4e13\u4e1a\u8dd1\u5305\u5b57\u5178\u4e0b\u8f7d\"><\/p>\n<p>Cython\u5728NLP\u4e2d\u7684\u52a0\u901f\u5e94\u7528  \u5f53\u6211\u4eec\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u5982\u4f55\u5728Cython\u4e2d\u8bbe\u8ba1\u66f4\u9ad8\u6548\u7684\u5faa\u73af\u5462\uff1fspaCy\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff01spaCy\u4e2d\u7684\u6240\u6709Unicode\u5b57\u7b26\u4e32\uff08\u6807\u8bb0\u7684\u6587\u672c\u3001\u5176\u5c0f\u5199\u6587\u672c\u3001\u5176\u8bcd\u5f62\u5f62\u5f0f\u3001POS\u6807\u7b7e\u3001\u89e3\u6790\u6811\u4f9d\u8d56\u6807\u7b7e\u3001\u547d\u540d\u5b9e\u4f53\u6807\u7b7e\u7b49\uff09\u90fd\u5b58\u50a8\u5728\u4e00\u4e2a\u79f0\u4e3aStringStore\u7684\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u5b83\u901a\u8fc7\u4e00\u4e2a64\u4f4d\u54c8\u5e0c\u7801\u8fdb\u884c\u7d22\u5f15\uff0c\u4f8b\u5982C\u7c7b\u578b\u7684uint64_t\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.ccwifi.cc\/blogs\/wp-content\/uploads\/2024\/03\/1711125778406_6.jpg\" alt=\"\u4e13\u4e1a\u8dd1\u5305\u5b57\u5178\u4e0b\u8f7d\"><\/p>\n<p>StringStore\u5bf9\u8c61\u5b9e\u73b0\u4e86Python Unicode\u5b57\u7b26\u4e32\u4e0e64\u4f4d\u54c8\u5e0c\u7801\u4e4b\u95f4\u7684\u67e5\u627e\u6620\u5c04\u3002\u5b83\u53ef\u4ee5\u5728spaCy\u7684\u4efb\u4f55\u5730\u65b9\u548c\u4efb\u610f\u5bf9\u8c61\u4e2d\u8bbf\u95ee\uff0c\u4f8b\u5982npl.vocab.strings\u3001doc.vocab.strings\u6216span.doc.vocab.string\u3002\u5f53\u67d0\u4e2a\u6a21\u5757\u9700\u8981\u5728\u67d0\u4e9b\u6807\u8bb0\u4e0a\u83b7\u5f97\u66f4\u5feb\u7684\u5904\u7406\u901f\u5ea6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528C\u8bed\u8a00\u7c7b\u578b\u768464\u4f4d\u54c8\u5e0c\u7801\u4ee3\u66ff\u5b57\u7b26\u4e32\u6765\u5b9e\u73b0\u3002\u8c03\u7528StringStore\u67e5\u627e\u8868\u5c06\u8fd4\u56de\u4e0e\u8be5\u54c8\u5e0c\u7801\u76f8\u5173\u8054\u7684Python Unicode\u5b57\u7b26\u4e32\u3002\u4f46\u662f\uff0cspaCy\u80fd\u505a\u7684\u4e0d\u4ec5\u4ec5\u662f\u8fd9\u4e9b\uff0c\u5b83\u8fd8\u5141\u8bb8\u6211\u4eec\u8bbf\u95ee\u5b8c\u5168\u586b\u5145\u7684C\u8bed\u8a00\u7c7b\u578b\u7ed3\u6784\u7684\u6587\u6863\u548c\u8bcd\u6c47\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Cython\u5faa\u73af\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u7ed3\u6784\uff0c\u800c\u4e0d\u5fc5\u6784\u5efa\u81ea\u5df1\u7684\u7ed3\u6784\u3002spaCy\u62d3\u5c55\uff1a<\/p>\n<pre style=\"margin:0;padding:0;border-radius:none;background:none\"><code style=\"border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;padding: 6px\" class=\"hljs-sublime\"><span style=\"font-size: 14px\"><span class=\"hljs-sublime-keyword\" style=\"width: 43px;font-weight: 400;font-style: normal\">import<\/span> urllib.request<\/span><br><span style=\"font-size: 14px\"><span class=\"hljs-sublime-keyword\" style=\"font-size: 14px;width: 43px;font-weight: 400;font-style: normal\">import<\/span> spacy<\/span><br><br><span style=\"font-size: 14px\"><span class=\"hljs-sublime-keyword\" style=\"font-size: 14px;width: 29px;font-weight: 400;font-style: normal\">with<\/span> urllib.request.urlopen(<span class=\"hljs-sublime-string\" style=\"font-size: 14px;width: 759px;font-weight: 400;font-style: normal\">'https:\/\/raw.githubusercontent.com\/pytorch\/examples\/master\/word_language_model\/data\/wikitext-2\/valid.txt'<\/span>) <span class=\"hljs-sublime-keyword\" style=\"font-size: 14px;width: 15px;font-weight: 400;font-style: normal\">as<\/span> response:<\/span><br><span style=\"font-size: 14px\"> text = response.read()<\/span><br><span style=\"font-size: 14px\">nlp = spacy.load(<span class=\"hljs-sublime-string\" style=\"font-size: 14px;width: 29px;font-weight: 400;font-style: normal\">'en'<\/span>)<\/span><br><span style=\"font-size: 14px\">doc_list = list(nlp(text[:<span class=\"hljs-sublime-number\" style=\"font-size: 14px;width: 43px;font-weight: 400;font-style: normal\">800000<\/span>].decode(<span class=\"hljs-sublime-string\" style=\"font-size: 14px;width: 43px;font-weight: 400;font-style: normal\">'utf8'<\/span>)) <span class=\"hljs-sublime-keyword\" style=\"font-size: 14px;width: 22px;font-weight: 400;font-style: normal\">for<\/span> i <span class=\"hljs-sublime-keyword\" style=\"font-size: 14px;width: 15px;font-weight: 400;font-style: normal\">in<\/span> range(<span class=\"hljs-sublime-number\" style=\"font-size: 14px;width: 15px;font-weight: 400;font-style: normal\">10<\/span>))<\/span><\/code><\/pre>\n<p>\u5efa\u7acb\u4e00\u4e2a\u811a\u672c\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u5305\u542b10\u4efd\u6587\u6863\u7684\u5217\u8868\uff0c\u6bcf\u4efd\u6587\u6863\u5927\u7ea6\u5305\u542b17\u4e07\u4e2a\u5355\u8bcd\uff0c\u4f7f\u7528spaCy\u8fdb\u884c\u5206\u6790\u3002\u5f53\u7136\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5bf917\u4e07\u4efd\u6587\u6863\uff08\u6bcf\u4efd\u6587\u6863\u5305\u542b10\u4e2a\u5355\u8bcd\uff09\u8fdb\u884c\u5206\u6790\uff0c\u4f46\u8fd9\u6837\u505a\u4f1a\u5bfc\u81f4\u521b\u5efa\u8fc7\u7a0b\u975e\u5e38\u6162\uff0c\u56e0\u6b64\u6211\u4eec\u9009\u62e9\u4e8610\u4efd\u6587\u6863\u3002\u6211\u4eec\u60f3\u8981\u5728\u8fd9\u4e2a\u6570\u636e\u96c6\u4e0a\u6267\u884c\u67d0\u4e9b\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u7edf\u8ba1\u6570\u636e\u96c6\u4e2d\u4f5c\u4e3a\u540d\u8bcd\u51fa\u73b0\u7684\u5355\u8bcd&#8221;run&#8221;\u7684\u6b21\u6570\uff08\u4f8b\u5982\uff0c\u88abspaCy\u6807\u8bb0\u4e3a&#8221;NN&#8221;\u8bcd\u6027\u6807\u7b7e\uff09\u3002\u4f7f\u7528Python\u5faa\u73af\u6765\u5b9e\u73b0\u4e0a\u8ff0\u5206\u6790\u8fc7\u7a0b\u975e\u5e38\u7b80\u5355\u548c\u76f4\u89c2\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.ccwifi.cc\/blogs\/wp-content\/uploads\/2024\/03\/1711125778406_7.jpg\" alt=\"\u4e13\u4e1a\u8dd1\u5305\u5b57\u5178\u4e0b\u8f7d\"><\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u81f3\u5c11\u9700\u8981\u8fd0\u884c1.4\u79d2\u624d\u80fd\u83b7\u5f97\u7b54\u6848\u3002\u5982\u679c\u6211\u4eec\u7684\u6570\u636e\u96c6\u4e2d\u5305\u542b\u6570\u767e\u4e07\u4e2a\u6587\u6863\uff0c\u4e3a\u4e86\u83b7\u5f97\u7b54\u6848\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u82b1\u8d39\u8d85\u8fc7\u4e00\u5929\u7684\u65f6\u95f4\u3002\u6211\u4eec\u4e5f\u8bb8\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6765\u52a0\u901f\uff0c\u4f46\u5728Python\u4e2d\u8fd9\u79cd\u505a\u6cd5\u5e76\u4e0d\u660e\u667a\uff0c\u56e0\u4e3a\u60a8\u8fd8\u9700\u8981\u5904\u7406\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\u3002\u5728Cython\u4e2d\uff0c\u53ef\u4ee5\u65e0\u89c6GIL\u7684\u5b58\u5728\u800c\u81ea\u7531\u4f7f\u7528\u7ebf\u7a0b\u52a0\u901f\u3002\u4f46\u662f\u4e0d\u80fd\u518d\u4f7f\u7528Python\u4e2d\u7684\u5b57\u5178\u548c\u5217\u8868\uff0c\u56e0\u4e3aPython\u4e2d\u7684\u53d8\u91cf\u90fd\u81ea\u52a8\u5e26\u6709\u9501\uff08GIL\uff09\u3002\u5e78\u8fd0\u7684\u662f\uff0cCython\u5df2\u7ecf\u5c01\u88c5\u4e86C++\u6807\u51c6\u5e93\u4e2d\u7684\u5bb9\u5668\uff1adeque\u3001list\u3001map\u3001pair\u3001queue\u3001set\u3001stack\u3001vector\u3002\u5b8c\u5168\u53ef\u4ee5\u66ff\u4ee3Python\u7684dict\u3001list\u3001set\u7b49\u3002\u6211\u4eec\u4f7f\u7528Cython\u5c31\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f46\u4e0d\u80fd\u518d\u4f7f\u7528Python\u4e2d\u7684\u5b57\u5178\u548c\u5217\u8868\uff0c\u56e0\u4e3aPython\u4e2d\u7684\u53d8\u91cf\u90fd\u81ea\u52a8\u5e26\u6709\u9501\uff08GIL\uff09\u3002\u5e78\u8fd0\u7684\u662f\uff0cCython\u5df2\u7ecf\u5c01\u88c5\u4e86C++\u6807\u51c6\u5e93\u4e2d\u7684\u5bb9\u5668\uff1adeque\u3001list\u3001map\u3001pair\u3001queue\u3001set\u3001stack\u3001vector\u3002\u5b8c\u5168\u53ef\u4ee5\u66ff\u4ee3Python\u7684dict\u3001list\u3001set\u7b49\u3002\u53e6\u5916\uff0c\u8bf7\u6ce8\u610f\uff0cCython\u4e5f\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\uff01Cython\u5728\u540e\u53f0\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528OpenMP\u3002\u73b0\u5728\u8ba9\u6211\u4eec\u5c1d\u8bd5\u4f7f\u7528spaCy\u548cCython\u6765\u52a0\u901fPython\u4ee3\u7801\u3002\u9996\u5148\u9700\u8981\u8003\u8651\u597d\u6570\u636e\u7ed3\u6784\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2aC\u7c7b\u578b\u7684\u6570\u7ec4\u6765\u5b58\u50a8\u6570\u636e\uff0c\u9700\u8981\u6307\u9488\u6765\u6307\u5411\u6bcf\u4e2a\u6587\u6863\u7684TokenC\u6570\u7ec4\u3002\u6211\u4eec\u8fd8\u9700\u8981\u5c06\u6d4b\u8bd5\u5b57\u7b26\uff08&#8221;run&#8221;\u548c&#8221;NN&#8221;\uff09\u8f6c\u6362\u4e3a64\u4f4d\u54c8\u5e0c\u7801\u3002\u5f53\u6240\u6709\u9700\u8981\u5904\u7406\u7684\u6570\u636e\u90fd\u53d8\u6210\u4e86C\u7c7b\u578b\u5bf9\u8c61\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4ee5\u7eafC\u8bed\u8a00\u7684\u901f\u5ea6\u5bf9\u6570\u636e\u96c6\u8fdb\u884c\u8fed\u4ee3\u3002\u4ee5\u4e0b\u662f\u8f6c\u6362\u4e3aCython\u548cspaCy\u7684\u5b9e\u73b0\uff1a<\/p>\n<pre style=\"margin:0;padding:0;border-radius:none;background:none\"><code style=\"border-radius: 4px;font-size: 0.85em;margin: 0px 0.15em;padding: 6px\" class=\"hljs-sublime\"><span style=\"font-size: 14px\">%%cython<\/span><br><span style=\"font-size: 14px\">cdef extern <span class=\"hljs-sublime-keyword\" style=\"font-size: 14px;width: 29px;font-weight: 400;font-style: normal\">from<\/span> <span class=\"hljs-sublime-string\" style=\"font-size: 14px;width: 57px;font-weight: 400;font-style: normal\">\"math.h\"<\/span>:<\/span><br> <span style=\"font-size: 14px\">&nbsp;&nbsp;&nbsp;&nbsp;cpdef double sin(double x)<\/span><\/code><\/pre>\n<p>\u5728Jupyter notebook\u4e0a\uff0c\u8fd9\u6bb5Cython\u4ee3\u7801\u8fd0\u884c\u4e86\u5927\u7ea620\u6beb\u79d2\uff0c\u6bd4\u4e4b\u524d\u7684\u7eafPython\u5faa\u73af\u5feb\u4e86\u5927\u7ea680\u500d\u3002\u4f7f\u7528Jupyter notebook\u5355\u5143\u7f16\u5199\u6a21\u5757\u7684\u901f\u5ea6\u5f88\u53ef\u89c2\uff0c\u5b83\u53ef\u4ee5\u4e0e\u5176\u4ed6Python\u6a21\u5757\u548c\u51fd\u6570\u81ea\u7136\u5730\u8fde\u63a5\uff1a\u572820\u6beb\u79d2\u5185\u626b\u63cf\u5927\u7ea6170\u4e07\u4e2a\u5355\u8bcd\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u6bcf\u79d2\u80fd\u591f\u5904\u7406\u9ad8\u8fbe8\u5343\u4e07\u4e2a\u5355\u8bcd\u3002\u5982\u679c\u60a8\u5df2\u7ecf\u4e86\u89e3C\u8bed\u8a00\uff0cCython\u8fd8\u5141\u8bb8\u8bbf\u95eeC\u4ee3\u7801\uff0c\u800cCython\u7684\u521b\u5efa\u8005\u8fd8\u6ca1\u6709\u4e3a\u8fd9\u4e9b\u4ee3\u7801\u6dfb\u52a0\u73b0\u6210\u7684\u58f0\u660e\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff0c\u53ef\u4ee5\u4e3aC\u51fd\u6570\u751f\u6210Python\u5305\u88c5\u5668\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u6a21\u5757dict\u4e2d\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.ccwifi.cc\/blogs\/wp-content\/uploads\/2024\/03\/1711125778406_8.jpg\" alt=\"\u4e13\u4e1a\u8dd1\u5305\u5b57\u5178\u4e0b\u8f7d\"><\/p>\n<p>Cython\u6ce8\u610f\u7684\u9677\u9631  1\u3001.pyx\u4e2d\u7528CDEF\u5b9a\u4e49\u7684\u5185\u5bb9\uff0c\u9664\u7c7b\u4ee5\u5916\u5bf9\u7684.py\u90fd\u662f\u4e0d\u53ef\u89c1\u7684\u3002  2\u3001.c\u4e2d\u4e0d\u80fd\u64cd\u4f5cC\u7c7b\u578b\uff0c\u5982\u679c\u60f3\u5728.py\u4e2d\u64cd\u4f5cC\u7c7b\u578b\uff0c\u5c31\u8981\u5728.pyx\u4e2d\u4ecePython\u5bf9\u8c61\u8f6c\u6362\u4e3aC\u7c7b\u578b\uff0c\u6216\u8005\u7528\u542b\u6709set\/get\u65b9\u6cd5\u7684C\u7c7b\u578b\u5305\u88c5\u7c7b\u3002  3\u3001\u867d\u7136Cython\u53ef\u4ee5\u81ea\u52a8\u8fdb\u884cPython\u7684str\u548cC\u7684&#8221;char *&#8221;\u4e4b\u95f4\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u4f46\u5bf9\u4e8e\u56fa\u5b9a\u957f\u5ea6\u7684\u5b57\u7b26\u4e32&#8221;char a[n]&#8221;\u662f\u65e0\u6cd5\u81ea\u52a8\u8f6c\u6362\u7684\u3002\u9700\u8981\u4f7f\u7528Cython\u7684libc.string.strcpy\u8fdb\u884c\u663e\u5f0f\u62f7\u8d1d\u3002  4\u3001\u56de\u8c03\u51fd\u6570\u9700\u8981\u7528\u51fd\u6570\u5305\u88c5\uff0c\u5e76\u901a\u8fc7C\u7684&#8221;void *&#8221;\u5f3a\u5236\u8f6c\u6362\u540e\u624d\u80fd\u4f20\u5165C\u51fd\u6570\u3002  Cython\u76f8\u5173\u8d44\u6599\uff08\u4e0b\u8f7d\uff09  0\u3001\u5176\u4ed6\uff1a  <\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.ccwifi.cc\/blogs\/wp-content\/uploads\/2024\/03\/1711125778406_9.png\" alt=\"\u4e13\u4e1a\u8dd1\u5305\u5b57\u5178\u4e0b\u8f7d\"><img decoding=\"async\" src=\"http:\/\/www.ccwifi.cc\/blogs\/wp-content\/uploads\/2024\/03\/1711125778406_10.jpg\" alt=\"\u4e13\u4e1a\u8dd1\u5305\u5b57\u5178\u4e0b\u8f7d\"><img decoding=\"async\" src=\"http:\/\/www.ccwifi.cc\/blogs\/wp-content\/uploads\/2024\/03\/1711125778406_11.jpg\" alt=\"\u4e13\u4e1a\u8dd1\u5305\u5b57\u5178\u4e0b\u8f7d\"><img decoding=\"async\" src=\"http:\/\/www.ccwifi.cc\/blogs\/wp-content\/uploads\/2024\/03\/1711125778406_12.jpg\" alt=\"\u4e13\u4e1a\u8dd1\u5305\u5b57\u5178\u4e0b\u8f7d\"><\/p>\n<p>1\u3001\u5b98\u65b9\u6587\u6863\uff1a  2\u3001\u53c2\u8003\u4e66\u7c4d\uff08\u6587\u672b\u4e0b\u8f7d\uff09\uff1a  \u4e66\u7c4d\u4e0b\u8f7d  \u5728\u540e\u53f0\u8f93\u5165\uff08\u4e25\u683c\u5927\u5c0f\u5199\uff09  Cython\u8d44\u6599  \u2014End\u2014  \u91cf\u5316\u6295\u8d44\u4e0e\u673a\u5668\u5b66\u4e60\u5fae\u4fe1\u516c\u4f17\u53f7\uff0c\u662f\u4e1a\u5185\u5782\u76f4\u4e8eQuant\u3001MFE\u3001CST\u3001AI\u7b49\u4e13\u4e1a\u7684\u4e3b\u6d41\u91cf\u5316\u81ea\u5a92\u4f53\u3002\u516c\u4f17\u53f7\u62e5\u6709\u6765\u81ea\u516c\u52df\u3001\u79c1\u52df\u3001\u5238\u5546\u3001\u94f6\u884c\u3001\u6d77\u5916\u7b49\u4f17\u591a\u5708\u518510W+\u5173\u6ce8\u8005\u3002\u6bcf\u65e5\u53d1\u5e03\u884c\u4e1a\u524d\u6cbf\u7814\u7a76\u6210\u679c\u548c\u6700\u65b0\u91cf\u5316\u8d44\u8baf\u3002<\/p>\n<p><!-- \u6587\u7ae0\u6765\u6e90:http:\/\/mp.weixin.qq.com\/s?src=11&amp;timestamp=1711125754&amp;ver=5154&amp;signature=Mi9jYOuyLic13p2Muma6KYEq-Sfbws9Kby1nlGkBPzl1qRY*RtVOCFyomjxvsyWnXWjIQu5e7l1lKdJyM-vSNkJcdOpinJxczwDzlZ3DGKBQY8q1CA3bGZxDCtMwQ4gm&amp;new=1 --><\/p>\n","protected":false},"excerpt":{"rendered":"<p>30\u500d\uff01\u4f7f\u7528Cython\u52a0\u901fPython\u4ee3\u7801\u6807\u661f\u2605\u516c\u4f17\u53f7  \u7231\u4f60\u4eec\u2665\u4f5c\u8005\uff1aGeorge Seif\u3001Thomas Wolf\u3001Lukas Frei\u7f16\u8bd1\uff1a1+1=6 | \u516c\u4f17\u53f7\u6d77\u5916\u90e8\u8fd1\u671f\u539f\u521b\u6587\u7ae0\uff1a\u2665\u2665\u26652\u4e07\u5b57\u5e72\u8d27\uff1a\u2665\u2665\u2665\u2665\u2665\u2665\u2665\u2665\u2665\u2665\u2665\u2665<\/p>\n","protected":false},"author":1,"featured_media":2816,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"topic":[],"class_list":["post-2815","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-1"],"_links":{"self":[{"href":"https:\/\/www.ccwifi.cc\/blogs\/wp-json\/wp\/v2\/posts\/2815","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ccwifi.cc\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccwifi.cc\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccwifi.cc\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccwifi.cc\/blogs\/wp-json\/wp\/v2\/comments?post=2815"}],"version-history":[{"count":0,"href":"https:\/\/www.ccwifi.cc\/blogs\/wp-json\/wp\/v2\/posts\/2815\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ccwifi.cc\/blogs\/wp-json\/wp\/v2\/media\/2816"}],"wp:attachment":[{"href":"https:\/\/www.ccwifi.cc\/blogs\/wp-json\/wp\/v2\/media?parent=2815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccwifi.cc\/blogs\/wp-json\/wp\/v2\/categories?post=2815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccwifi.cc\/blogs\/wp-json\/wp\/v2\/tags?post=2815"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.ccwifi.cc\/blogs\/wp-json\/wp\/v2\/topic?post=2815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}