{"id":643,"date":"2021-06-12T06:26:00","date_gmt":"2021-06-12T06:26:00","guid":{"rendered":"https:\/\/tensor.agenthub.uk\/?p=643"},"modified":"2024-05-17T06:27:04","modified_gmt":"2024-05-17T06:27:04","slug":"%e9%9c%87%e6%83%8a%ef%bc%81%e5%8a%a0%e9%80%9fx%e4%b8%aa%e6%95%b0%e9%87%8f%e7%ba%a7%ef%bc%81cython%e5%88%b0%e5%ba%95%e6%9c%89%e5%a4%9a%e5%bf%ab%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/tensorzen.blog\/?p=643","title":{"rendered":"\u9707\u60ca\uff01\u52a0\u901fX\u4e2a\u6570\u91cf\u7ea7\uff01Cython\u5230\u5e95\u6709\u591a\u5feb\uff1f"},"content":{"rendered":"\n<p>\u76ee\u524d\u6765\u8bf4\uff0c\u5904\u7406\u6570\u636e\u6211\u8fd8\u662f\u6bd4\u8f83\u559c\u6b22\u4f7f\u7528pandas\uff0c\u786e\u5b9e\u723d\u5230\u98de\u8d77\uff0c\u800c\u8fd9\u79cd\u6613\u7528\u6027\u5e26\u6765\u7684\u662f\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u7684\u590d\u6742\u6027\uff0c\u4ece\u800c\u5bfc\u81f4\u6027\u80fd\u635f\u5931\u3002\u4f46\u662f\u597d\u7528\u771f\u7684\u592a\u91cd\u8981\u4e86\uff0c\u6240\u4ee5\u51fa\u73b0\u4e86\u50cfNumba\/Datatable\u7b49\u4e00\u4e9b\u5217\u652f\u6301\u5e76\u884c\u5316\u7684Dataframe\u7684\u65b9\u6848\uff0c\u5f53\u7136\u4e5f\u6709dask\u8fd9\u4e00\u7c7b\u5206\u5e03\u5f0f\u5e76\u884c\u67b6\u6784\uff0c\u8bdd\u8bf4\u56de\u6765 Numba\u5df2\u7ecf\u652f\u6301GPU\u52a0\u901f\uff5e\u8bf4\u8d77\u6765\u8fd8\u662f\u633a\u723d\u7684\uff0c\u4f46\u662f\u8fd9\u4e0d\u662f\u6587\u7ae0\u7684\u91cd\u70b9\uff0c\u5173\u4e8epandas\u5e76\u884c\uff0c\u524d\u9762\u5df2\u7ecf\u6709\u4e00\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u8fc7\u4e86\u3002<\/p>\n\n\n\n<p>\u8fd9\u6b21\u6765\u6211\u4eec\u804a\u4e00\u4e0b\u52a0\u901fPython\u7684\u53e6\u4e00\u79cd\u601d\u8def\uff0cC\u8bed\u8a00\u52a0\u901f\uff0c\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528Cython\uff0csklearn\u5927\u591a\u6570\u8ba1\u7b97\u7531\u8fd9\u4e2a\u65b9\u6848\u5b9e\u73b0\u3002\u5f53\u7136\u53ef\u4ee5\u5229\u7528C\u8bed\u8a00\u76f4\u63a5\u6269\u5c55python,PyTorch\/TensorFlow\/Numpy\u90fd\u662f\u8fd9\u79cd\u65b9\u6848\uff0c\u4f46\u662f\u8fd9\u4e2a\u9700\u8981\u5bf9C\/C++\u5f00\u53d1\u6bd4\u8f83\u719f\u6089\uff0c\u5f00\u53d1\u6548\u7387\u53ef\u80fd\u662f\u4e0d\u53caCython\u7684\uff0c\u53ef\u80fd\u54c8\u3002<\/p>\n\n\n\n<p>\u4e3a\u4ec0\u4e48python\u6162\uff0c\u56e0\u4e3a\u5b83\u662f\u52a8\u6001\u6570\u636e\u7c7b\u578b\uff0c\u8fd0\u884c\u65f6\u89e3\u91ca\u5668\u8981\u82b1\u8d39\u5927\u91cf\u65f6\u95f4\u6765\u786e\u5b9a\u5bf9\u8c61\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4ece\u800c\u5224\u5b9a\u6570\u636e\u7c7b\u578b\u7684\u5c5e\u6027\uff0cC\u8bed\u8a00\u7b49\u4e00\u4e9b\u5217\u4e25\u683c\u7684\u9759\u6001\u6570\u636e\u7c7b\u578b\u8bed\u8a00\u5c31\u6ca1\u6709\u8fd9\u4e9b\u9057\u61be\uff0c\u6240\u4ee5C\u7684\u6548\u7387\u8981\u9ad8\u5f88\u591a\uff0c\u5728\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\u4e0b\u80fd\u9ad8\u51fa\u51e0\u4e2a\u6570\u91cf\u7ea7\u3002Cython\u7684\u539f\u59cb\u6587\u6863\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>This can make Python a very relaxed and comfortable language for rapid development, but with a price &#8211; the \u2018red tape\u2019 of managing data types is dumped onto the interpreter. At run time, the interpreter does a lot of work searching namespaces, fetching attributes and parsing argument and keyword tuples. This run-time \u2018late binding\u2019 is a major cause of Python\u2019s relative slowness compared to \u2018early binding\u2019 languages such as C++.<\/p>\n<\/blockquote>\n\n\n\n<p>This\u6307\u7684\u662fpython\u7684\u52a8\u6001\u6570\u636e\u7c7b\u578b\u4f18\u52bf\u3002\u76f4\u63a5\u770b\u4f8b\u5b50\uff0c\u6765\u81ea\u8fd9\u7bc7\u6587\u7ae0\uff1ahttps:\/\/pythonprogramming.net\/introduction-and-basics-cython-tutorial<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(2 * 0.6 * .75rem);line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\n# example_original.py\ndef test(x):\n    y = 0\n    for i in range(x):\n        y += i\n    return y\n``````python\n# example_cython.pyx\ncpdef int test(int x):\n    cdef int y = 0\n    cdef int i\n    for i in range(x):\n        y += i\n    return y\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7B7F8B\"># example_original.py<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">def<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">test<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #FFB86C; font-style: italic\">x<\/span><span style=\"color: #F6F6F4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    y <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(x):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        y <\/span><span style=\"color: #F286C4\">+=<\/span><span style=\"color: #F6F6F4\"> i<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> y<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4; font-style: italic; text-decoration: underline\">``````<\/span><span style=\"color: #F6F6F4\">python<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7B7F8B\"># example_cython.pyx<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">cpdef <\/span><span style=\"color: #97E1F1; font-style: italic\">int<\/span><span style=\"color: #F6F6F4\"> test(<\/span><span style=\"color: #97E1F1; font-style: italic\">int<\/span><span style=\"color: #F6F6F4\"> x):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    cdef <\/span><span style=\"color: #97E1F1; font-style: italic\">int<\/span><span style=\"color: #F6F6F4\"> y <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    cdef <\/span><span style=\"color: #97E1F1; font-style: italic\">int<\/span><span style=\"color: #F6F6F4\"> i<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(x):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        y <\/span><span style=\"color: #F286C4\">+=<\/span><span style=\"color: #F6F6F4\"> i<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> y<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Cython\u6587\u4ef6\u540e\u7f00\u540d\u662f&#8221;.pyx&#8221;\uff0c\u76f8\u6bd4\u539f\u751fpython\u65b9\u6cd5\uff0c\u589e\u52a0\u4e86\u6570\u636e\u7c7b\u578b\u7684\u5b9a\u4e49\uff0c\u63a5\u4e0b\u6765\u9700\u8981\u7f16\u5199setup.py\u6587\u4ef6\u7528\u4e8e\u6784\u5efapyx\u6587\u4ef6<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .75rem);line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\nfrom distutils.core import setup\nfrom Cython.Build import cythonize\n\nsetup(ext_modules = cythonize('example_cython.pyx'))\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">from<\/span><span style=\"color: #F6F6F4\"> distutils.core <\/span><span style=\"color: #F286C4\">import<\/span><span style=\"color: #F6F6F4\"> setup<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">from<\/span><span style=\"color: #F6F6F4\"> Cython.Build <\/span><span style=\"color: #F286C4\">import<\/span><span style=\"color: #F6F6F4\"> cythonize<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">setup(<\/span><span style=\"color: #FFB86C; font-style: italic\">ext_modules<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> cythonize(<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">example_cython.pyx<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">))<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u4e09\u4e2a\u6587\u4ef6\u90fd\u662f\u5728\u540c\u4e00\u4e2a\u76ee\u5f55\u4e0b\uff0c\u8fdbshell\u6267\u884c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .75rem);line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\" python setup.py build_ext --inplace\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\"> python setup.py build_ext <\/span><span style=\"color: #EE6666; font-style: italic; text-decoration: underline\">--<\/span><span style=\"color: #F6F6F4\">inplace<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u4e00\u5207\u987a\u5229\u7684\u8bdd\uff5e\uff5e\u4f1a\u5f97\u5230\u4e00\u4e2aWarning\uff5e\uff5e<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>FutureWarning: Cython directive &#8216;language_level&#8217; not set, using 2 for now (Py2). This will change in a later release!<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u5728pyx\u6587\u4ef6\u4e2d\u52a0\u5165\u5982\u4e0b\u58f0\u660e\uff1a<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .75rem);line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"# cython: language_level=3\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #7B7F8B\"># cython: language_level=3<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u6ca1\u4ec0\u4e48\u95ee\u9898\u5c31OK\u4e86\uff0c\u63a5\u4e0b\u6765\u5199\u4e00\u4e2atest\u6587\u4ef6:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(2 * 0.6 * .75rem);line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"import example_cython, example_original, time\n\nif __name__ == '__main__':\n    times = 10000\n    add_times = 100\n\n    # original\n    original_total_elapse = 0.0\n    for i in range(times):\n        start_time = time.time()\n        example_original.test(add_times)\n        original_total_elapse += time.time() - start_time\n\n    # cython\n    cython_total_elapse = 0.0\n    for i in range(times):\n        start_time = time.time()\n        example_cython.test(add_times)\n        cython_total_elapse += time.time() - start_time\n\n    print(&quot;Cython is {}x faster.&quot;.format(original_total_elapse \/ cython_total_elapse) )\n\n\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">import<\/span><span style=\"color: #F6F6F4\"> example_cython, example_original, time<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">if<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">__name__<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">==<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">__main__<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    times <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">10000<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    add_times <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">100<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #7B7F8B\"># original<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    original_total_elapse <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">0.0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(times):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        start_time <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> time.time()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        example_original.test(add_times)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        original_total_elapse <\/span><span style=\"color: #F286C4\">+=<\/span><span style=\"color: #F6F6F4\"> time.time() <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> start_time<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #7B7F8B\"># cython<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    cython_total_elapse <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">0.0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(times):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        start_time <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> time.time()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        example_cython.test(add_times)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        cython_total_elapse <\/span><span style=\"color: #F286C4\">+=<\/span><span style=\"color: #F6F6F4\"> time.time() <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> start_time<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #97E1F1\">print<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">Cython is <\/span><span style=\"color: #BF9EEE\">{}<\/span><span style=\"color: #E7EE98\">x faster.<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\">.format(original_total_elapse <\/span><span style=\"color: #F286C4\">\/<\/span><span style=\"color: #F6F6F4\"> cython_total_elapse) )<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cython\u53ef\u4ee5\u5b9e\u73b0C\u8bed\u8a00\u52a0\u901fPython\u800c\u4e14\u4e0d\u7528\u5b66\u4e60C\u8bed\u8a00\uff0c\u8ba9\u6211\u4eec\u6765\u5b9e\u9a8c\u4e00\u4e0b\u5b83\u5230\u5e95\u53ef\u4ee5\u52a0\u5feb\u591a\u5c11\u500d\u7684\u8fd0\u884c\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[34],"class_list":["post-643","post","type-post","status-publish","format-standard","hentry","category-coding","tag-python"],"_links":{"self":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts\/643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=643"}],"version-history":[{"count":5,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts\/643\/revisions"}],"predecessor-version":[{"id":648,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts\/643\/revisions\/648"}],"wp:attachment":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}