{"id":719,"date":"2024-05-05T22:07:19","date_gmt":"2024-05-05T13:07:19","guid":{"rendered":"https:\/\/chocottopro.com\/?p=719"},"modified":"2024-05-05T22:07:19","modified_gmt":"2024-05-05T13:07:19","slug":"%e3%80%90cython%e6%9c%80%e6%96%b0%e5%8b%95%e5%90%91%e3%80%91%e6%a9%9f%e6%a2%b0%e5%ad%a6%e7%bf%92%e3%81%ae%e9%ab%98%e9%80%9f%e5%8c%96%e3%82%92%e5%ae%9f%e7%8f%be%ef%bc%81numpy%e3%82%84pandas%e3%81%a8","status":"publish","type":"post","link":"https:\/\/chocottopro.com\/?p=719","title":{"rendered":"\u3010Cython\u6700\u65b0\u52d5\u5411\u3011\u6a5f\u68b0\u5b66\u7fd2\u306e\u9ad8\u901f\u5316\u3092\u5b9f\u73fe\uff01NumPy\u3084Pandas\u3068\u306e\u9023\u643a\u6cd5\u3082\u89e3\u8aac"},"content":{"rendered":"\n<p>Python\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u98db\u8e8d\u7684\u306b\u5411\u4e0a\u3055\u305b\u308bCython\u3002\u305d\u306e\u57fa\u790e\u304b\u3089\u5fdc\u7528\u307e\u3067\u3001\u5b9f\u8df5\u7684\u306a\u30c6\u30af\u30cb\u30c3\u30af\u3092\u4ea4\u3048\u3066\u5fb9\u5e95\u89e3\u8aac\u3057\u307e\u3059\u3002\u6a5f\u68b0\u5b66\u7fd2\u3084\u79d1\u5b66\u8a08\u7b97\u306e\u5206\u91ce\u3067\u6ce8\u76ee\u3055\u308c\u308bCython\u306e\u9b45\u529b\u3092\u3001\u8c4a\u5bcc\u306a\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3068\u3068\u3082\u306b\u7d10\u89e3\u3044\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<div class=\"wp-block-sgb-block-simple sgb-box-simple sgb-box-simple--title-normal sgb-box-simple--with-border\"><div style=\"background-color:var(--wp--preset--color--sango-main);color:#FFF\" class=\"sgb-box-simple__title\">\u3053\u306e\u8a18\u4e8b\u3092\u8aad\u3093\u3060\u3089\u308f\u304b\u308b\u3053\u3068<\/div><div class=\"sgb-box-simple__body\" style=\"border-color:var(--wp--preset--color--sango-main);background-color:#FFF\">\n<p>Cython\u3068\u306f\u4f55\u304b\u3001\u305d\u306e\u57fa\u672c\u7684\u306a\u4ed5\u7d44\u307f\u3068\u5229\u70b9 <\/p>\n\n\n\n<p>Cython\u3068Python\u306e\u9023\u643a\u65b9\u6cd5\u3001\u9759\u7684\u578b\u4ed8\u3051\u306e\u6d3b\u7528\u6cd5 <\/p>\n\n\n\n<p>NumPy\u3001Pandas\u3001Scikit-learn\u306a\u3069\u306e\u6a5f\u68b0\u5b66\u7fd2\u30e9\u30a4\u30d6\u30e9\u30ea\u3068\u306e\u9ad8\u901f\u5316\u30c6\u30af\u30cb\u30c3\u30af <\/p>\n\n\n\n<p>\u30e1\u30e2\u30ea\u7ba1\u7406\u306e\u6700\u9069\u5316\u3001\u4e26\u5217\u51e6\u7406\u306e\u6d3b\u7528\u306a\u3069\u3001Cython\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\u306e\u30b3\u30c4 <\/p>\n\n\n\n<p>\u30c7\u30a3\u30fc\u30d7\u30e9\u30fc\u30cb\u30f3\u30b0\u3001\u30d3\u30c3\u30b0\u30c7\u30fc\u30bf\u5206\u6790\u3001Web\u958b\u767a\u306a\u3069\u3001\u69d8\u3005\u306a\u5206\u91ce\u3067\u306eCython\u6d3b\u7528\u4e8b\u4f8b <\/p>\n\n\n\n<p>Cython 3.0\u306e\u65b0\u6a5f\u80fd\u3001Pythran\u306a\u3069\u3001Cython\u306e\u4eca\u5f8c\u306e\u5c55\u671b<\/p>\n<\/div><\/div>\n\n\n\n<div class=\"toc\"><br \/>\n<b>Warning<\/b>:  Undefined array key \"is_admin\" in <b>\/home\/c7479301\/public_html\/chocottopro.com\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>116<\/b><br \/>\n<br \/>\n<b>Warning<\/b>:  Undefined array key \"is_category_top\" in <b>\/home\/c7479301\/public_html\/chocottopro.com\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>121<\/b><br \/>\n<br \/>\n<b>Warning<\/b>:  Undefined array key \"is_top\" in <b>\/home\/c7479301\/public_html\/chocottopro.com\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>128<\/b><br \/>\n    <div id=\"toc_container\" class=\"sgb-toc--bullets js-smooth-scroll\" data-dialog-title=\"Table of Contents\">\n      <p class=\"toc_title\">\u76ee\u6b21 <\/p>\n      <ul class=\"toc_list\">  <li class=\"first\">    <a href=\"#i-0\">Cython\u3068\u306f\u4f55\u304b\uff1fPython\u3092\u9ad8\u901f\u5316\u3059\u308b\u305f\u3081\u306e\u9b54\u6cd5\u306e\u30c4\u30fc\u30eb<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-1\">Cython\u306e\u57fa\u672c\u6982\u5ff5\u3068\u4ed5\u7d44\u307f<\/a>      <\/li>      <li>        <a href=\"#i-2\">Cython\u3068Python\u306e\u9055\u3044 \u2013 \u9759\u7684\u578b\u4ed8\u3051\u3068\u30b3\u30f3\u30d1\u30a4\u30eb<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-3\">Cython\u3092\u4f7f\u3046\u30e1\u30ea\u30c3\u30c8 \u2013 \u9a5a\u304f\u3079\u304d\u9ad8\u901f\u5316\u3092\u5b9f\u73fe<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-4\">Cython\u306e\u4f7f\u3044\u65b9 \u2013 \u30b7\u30f3\u30d7\u30eb\u306a\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3067\u5b66\u3076<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-5\">Cython\u3068Python\u306e\u9023\u643a\u65b9\u6cd5 \u2013 pxd\u30d5\u30a1\u30a4\u30eb\u3068pyx\u30d5\u30a1\u30a4\u30eb<\/a>      <\/li>      <li>        <a href=\"#i-6\">\u578b\u5ba3\u8a00\u3068cdef\u306e\u4f7f\u3044\u65b9 \u2013 \u9759\u7684\u578b\u4ed8\u3051\u306b\u3088\u308b\u9ad8\u901f\u5316<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-7\">Cython\u306e\u30b3\u30f3\u30d1\u30a4\u30eb\u65b9\u6cd5 \u2013 distutils\/setuptools\u306e\u8a2d\u5b9a<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-8\">Cython\u306b\u3088\u308b\u6a5f\u68b0\u5b66\u7fd2\u306e\u9ad8\u901f\u5316\u30c6\u30af\u30cb\u30c3\u30af<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-9\">NumPy\u3068Cython\u306e\u9023\u643a \u2013 \u9ad8\u901f\u306a\u30d9\u30af\u30c8\u30eb\/\u884c\u5217\u8a08\u7b97<\/a>      <\/li>      <li>        <a href=\"#i-10\">Pandas\u3068Cython\u306e\u9023\u643a \u2013 \u30c7\u30fc\u30bf\u524d\u51e6\u7406\u306e\u9ad8\u901f\u5316<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-11\">Scikit-learn\u3068Cython\u306e\u9023\u643a \u2013 \u30e2\u30c7\u30eb\u306e\u5b66\u7fd2\u3068\u4e88\u6e2c\u306e\u9ad8\u901f\u5316<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-12\">Cython\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\u306e\u30b3\u30c4<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-13\">\u30e1\u30e2\u30ea\u7ba1\u7406\u306e\u6700\u9069\u5316 \u2013 \u7121\u99c4\u306a\u30e1\u30e2\u30ea\u30b3\u30d4\u30fc\u3092\u524a\u6e1b<\/a>      <\/li>      <li>        <a href=\"#i-14\">\u30d0\u30a6\u30f3\u30c9\u30c1\u30a7\u30c3\u30af\u306e\u524a\u9664 \u2013 \u3055\u3089\u306a\u308b\u9ad8\u901f\u5316\u3092\u72d9\u3046<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-15\">\u4e26\u5217\u51e6\u7406\u306e\u6d3b\u7528 \u2013 \u30de\u30eb\u30c1\u30b3\u30a2CPU\u306e\u6027\u80fd\u3092\u5f15\u304d\u51fa\u3059<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-16\">Cython\u306e\u6d3b\u7528\u4e8b\u4f8b \u2013 \u5b9f\u969b\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u304b\u3089\u5b66\u3076<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-17\">Cython\u00d7\u30c7\u30a3\u30fc\u30d7\u30e9\u30fc\u30cb\u30f3\u30b0 \u2013 TensorFlow\u3084PyTorch\u306e\u9ad8\u901f\u5316<\/a>      <\/li>      <li>        <a href=\"#i-18\">Cython\u00d7\u30d3\u30c3\u30b0\u30c7\u30fc\u30bf\u5206\u6790 \u2013 PySpark\/Dask\u306e\u51e6\u7406\u3092\u9ad8\u901f\u5316<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-19\">Cython\u00d7Web\u958b\u767a \u2013 \u30dc\u30c8\u30eb\u30cd\u30c3\u30af\u3068\u306a\u308b\u51e6\u7406\u306e\u9ad8\u901f\u5316<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-20\">Cython\u306e\u3053\u308c\u304b\u3089 \u2013 \u6700\u65b0\u306e\u958b\u767a\u52d5\u5411\u3068\u4eca\u5f8c\u306e\u5c55\u671b<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-21\">Cython 3.0\u306e\u65b0\u6a5f\u80fd\u3068\u6539\u5584\u70b9<\/a>      <\/li>      <li>        <a href=\"#i-22\">Python\u3068Cython\u306e\u57a3\u6839\u304c\u3055\u3089\u306b\u4f4e\u304f\u306a\u308b\uff1fPythran\u767b\u5834<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-23\">\u4ed6\u8a00\u8a9e\u3068\u306e\u9023\u643a \u2013 Rust\/Julia\/Go\u3068\u306e\u6bd4\u8f03<\/a>      <\/li>    <\/ul>  <\/li>  <li class=\"last\">    <a href=\"#i-24\">\u307e\u3068\u3081 \u2013 Cython\u30de\u30b9\u30bf\u30fc\u3078\u306e\u7b2c\u4e00\u6b69<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-25\">Cython\u306e\u30e1\u30ea\u30c3\u30c8\u3068\u6ce8\u610f\u70b9\u306e\u518d\u78ba\u8a8d<\/a>      <\/li>      <li>        <a href=\"#i-26\">Cython\u30b9\u30ad\u30eb\u30a2\u30c3\u30d7\u306e\u305f\u3081\u306e\u30ea\u30bd\u30fc\u30b9\u96c6<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-27\">Python\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6539\u5584\u306bCython\u3092\u6d3b\u7528\u3057\u3088\u3046\uff01<\/a>      <\/li>    <\/ul>  <\/li><\/ul>\n      \n    <\/div><\/div><h2 class=\"wp-block-heading\" id=\"i-0\">Cython\u3068\u306f\u4f55\u304b\uff1fPython\u3092\u9ad8\u901f\u5316\u3059\u308b\u305f\u3081\u306e\u9b54\u6cd5\u306e\u30c4\u30fc\u30eb<\/h2>\n\n\n\n<p>Cython\u306f\u3001Python\u3068C\u8a00\u8a9e\u306e\u9593\u306e\u30d6\u30ea\u30c3\u30b8\u3068\u306a\u308b\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u306e\u30b9\u30bf\u30c6\u30a3\u30c3\u30af\u30b3\u30f3\u30d1\u30a4\u30e9\u3067\u3059\u3002Python\u306e\u30b3\u30fc\u30c9\u306b\u9759\u7684\u578b\u4ed8\u3051\u3092\u8ffd\u52a0\u3057\u3001C\u306e\u30b3\u30fc\u30c9\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u3067\u3001Python\u306e\u5b9f\u884c\u901f\u5ea6\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u8a08\u7b97\u91cf\u306e\u591a\u3044\u51e6\u7406\u3084\u30dc\u30c8\u30eb\u30cd\u30c3\u30af\u3068\u306a\u308b\u90e8\u5206\u3092Cython\u3067\u9ad8\u901f\u5316\u3059\u308b\u3053\u3068\u3067\u3001Python\u30d7\u30ed\u30b0\u30e9\u30e0\u5168\u4f53\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u6539\u5584\u3067\u304d\u308b\u306e\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-1\">Cython\u306e\u57fa\u672c\u6982\u5ff5\u3068\u4ed5\u7d44\u307f<\/h3>\n\n\n\n<p>Cython\u306e\u57fa\u672c\u7684\u306a\u4ed5\u7d44\u307f\u306f\u3001Python\u306e\u30b3\u30fc\u30c9\u306b\u578b\u60c5\u5831\u3092\u4ed8\u4e0e\u3057\u3001Cython\u306e\u30b3\u30fc\u30c9\uff08.pyx\uff09\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u305d\u3057\u3066\u3001\u3053\u306eCython\u306e\u30b3\u30fc\u30c9\u3092C\u30b3\u30f3\u30d1\u30a4\u30e9\u3067\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u3001Python\u304b\u3089\u5229\u7528\u53ef\u80fd\u306a\u62e1\u5f35\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u751f\u6210\u3057\u307e\u3059\u3002\u751f\u6210\u3055\u308c\u305f\u62e1\u5f35\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3059\u308b\u3053\u3068\u3067\u3001\u9ad8\u901f\u5316\u3055\u308c\u305f\u30b3\u30fc\u30c9\u3092Python\u30d7\u30ed\u30b0\u30e9\u30e0\u304b\u3089\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>Cython\u306e\u4e3b\u8981\u306a\u6a5f\u80fd\u3068\u7279\u5fb4\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9759\u7684\u578b\u4ed8\u3051\uff1aPython\u306e\u30c0\u30a4\u30ca\u30df\u30c3\u30af\u578b\u4ed8\u3051\u306b\u52a0\u3048\u3066\u3001C\u8a00\u8a9e\u306e\u3088\u3046\u306a\u9759\u7684\u578b\u4ed8\u3051\u3092\u5c0e\u5165<\/li>\n\n\n\n<li>C\u3068\u306e\u9023\u643a\uff1aC\u306e\u95a2\u6570\u3084\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u76f4\u63a5\u5229\u7528\u53ef\u80fd\u3067\u3001Python\u306e\u62e1\u5f35\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u5bb9\u6613\u306b\u4f5c\u6210\u3067\u304d\u308b<\/li>\n\n\n\n<li>\u30e1\u30e2\u30ea\u7ba1\u7406\uff1aPython\u306e\u30ac\u30d9\u30fc\u30b8\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306b\u4f9d\u5b58\u305b\u305a\u3001\u624b\u52d5\u3067\u306e\u30e1\u30e2\u30ea\u7ba1\u7406\u304c\u53ef\u80fd<\/li>\n\n\n\n<li>\u4e26\u5217\u5316\uff1aOpenMP\u3084multiprocessing\u3092\u7528\u3044\u305f\u4e26\u5217\u51e6\u7406\u3092\u6d3b\u7528\u3067\u304d\u308b<\/li>\n<\/ul>\n\n\n\n<p>\u4ee5\u4e0b\u306f\u3001Cython\u306e\u57fa\u672c\u7684\u306a\u30b3\u30fc\u30c9\u4f8b\u3067\u3059\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># sample.pyx\ndef sum_squares(int n):\n    cdef int i, total = 0\n    for i in range(n):\n        total += i * i\n    return total<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># setup.py\nfrom distutils.core import setup\nfrom Cython.Build import cythonize\n\nsetup(\n    ext_modules = cythonize(\"sample.pyx\")\n)<\/pre>\n\n\n\n<p>\u30b3\u30f3\u30d1\u30a4\u30eb\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python setup.py build_ext --inplace<\/pre>\n\n\n\n<p>Python\u304b\u3089\u306e\u5229\u7528\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import sample\n\nresult = sample.sum_squares(1000000)\nprint(result)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-2\">Cython\u3068Python\u306e\u9055\u3044 \u2013 \u9759\u7684\u578b\u4ed8\u3051\u3068\u30b3\u30f3\u30d1\u30a4\u30eb<\/h3>\n\n\n\n<p>Cython\u3068Python\u306e\u4e3b\u306a\u9055\u3044\u306f\u3001\u9759\u7684\u578b\u4ed8\u3051\u3068\u30b3\u30f3\u30d1\u30a4\u30eb\u306b\u3042\u308a\u307e\u3059\u3002Python\u306f\u30c0\u30a4\u30ca\u30df\u30c3\u30af\u578b\u4ed8\u3051\u3092\u63a1\u7528\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u5909\u6570\u306e\u578b\u306f\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u5b9f\u884c\u6642\u306b\u6c7a\u5b9a\u3055\u308c\u307e\u3059\u3002\u4e00\u65b9\u3001Cython\u3067\u306f\u9759\u7684\u578b\u4ed8\u3051\u3092\u5c0e\u5165\u3059\u308b\u3053\u3068\u3067\u3001\u30b3\u30f3\u30d1\u30a4\u30eb\u6642\u306b\u5909\u6570\u306e\u578b\u304c\u6c7a\u5b9a\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u30b3\u30fc\u30c9\u306e\u5b9f\u884c\u901f\u5ea6\u304c\u5411\u4e0a\u3059\u308b\u306e\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u307e\u305f\u3001Python\u306f\u30a4\u30f3\u30bf\u30fc\u30d7\u30ea\u30bf\u8a00\u8a9e\u3067\u3059\u304c\u3001Cython\u306f\u30b3\u30f3\u30d1\u30a4\u30eb\u8a00\u8a9e\u3067\u3059\u3002Cython\u306e\u30b3\u30fc\u30c9\u306f\u3001C\u30b3\u30f3\u30d1\u30a4\u30e9\u306b\u3088\u3063\u3066\u30b3\u30f3\u30d1\u30a4\u30eb\u3055\u308c\u3001\u6a5f\u68b0\u8a9e\u306b\u5909\u63db\u3055\u308c\u307e\u3059\u3002\u3053\u306e\u30b3\u30f3\u30d1\u30a4\u30eb\u306e\u904e\u7a0b\u3067\u3001\u69d8\u3005\u306a\u6700\u9069\u5316\u304c\u884c\u308f\u308c\u3001Python\u306e\u30a4\u30f3\u30bf\u30fc\u30d7\u30ea\u30bf\u3092\u4ecb\u3055\u305a\u306b\u76f4\u63a5\u5b9f\u884c\u3055\u308c\u308b\u305f\u3081\u3001\u9ad8\u901f\u5316\u304c\u5b9f\u73fe\u3055\u308c\u308b\u306e\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-3\">Cython\u3092\u4f7f\u3046\u30e1\u30ea\u30c3\u30c8 \u2013 \u9a5a\u304f\u3079\u304d\u9ad8\u901f\u5316\u3092\u5b9f\u73fe<\/h3>\n\n\n\n<p>Cython\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001Python\u306e\u5b9f\u884c\u901f\u5ea6\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u7279\u306b\u3001\u6570\u5024\u8a08\u7b97\u3084\u30c7\u30fc\u30bf\u51e6\u7406\u306a\u3069\u3001\u8a08\u7b97\u91cf\u306e\u591a\u3044\u51e6\u7406\u3092\u542b\u3080Python\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u306f\u3001Cython\u306b\u3088\u308b\u9ad8\u901f\u5316\u306e\u52b9\u679c\u304c\u9855\u8457\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u4f8b\u3048\u3070\u3001\u4e0a\u8a18\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3067\u793a\u3057\u305f<code>sum_squares<\/code>\u95a2\u6570\u306f\u3001Cython\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001Python\u306e\u6a19\u6e96\u5b9f\u88c5\u306b\u6bd4\u3079\u3066\u6570\u5341\u500d\u304b\u3089\u6570\u767e\u500d\u306e\u901f\u5ea6\u5411\u4e0a\u304c\u671f\u5f85\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u9ad8\u901f\u5316\u306b\u3088\u308a\u3001\u5927\u898f\u6a21\u30c7\u30fc\u30bf\u306e\u51e6\u7406\u3084\u30b7\u30df\u30e5\u30ec\u30fc\u30b7\u30e7\u30f3\u306a\u3069\u3001\u3053\u308c\u307e\u3067Python\u3067\u306f\u5b9f\u7528\u7684\u3067\u306a\u304b\u3063\u305f\u5206\u91ce\u306b\u3082Python\u3092\u9069\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3055\u3089\u306b\u3001Cython\u3092\u4f7f\u3048\u3070\u3001Python\u3068C\u3092 seamless\u306b\u9023\u643a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002C\u306e\u95a2\u6570\u3084\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u76f4\u63a5\u5229\u7528\u3067\u304d\u308b\u305f\u3081\u3001\u65e2\u5b58\u306eC\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u6d3b\u7528\u3057\u305f\u308a\u3001Python\u306e\u62e1\u5f35\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u5bb9\u6613\u306b\u4f5c\u6210\u3057\u305f\u308a\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u306e\u3088\u3046\u306b\u3001Cython\u306f\u3001Python\u306e\u6301\u3064\u958b\u767a\u306e\u5bb9\u6613\u3055\u3068\u751f\u7523\u6027\u3092\u640d\u306a\u3046\u3053\u3068\u306a\u304f\u3001C\u8a00\u8a9e\u306b\u8feb\u308b\u9ad8\u901f\u6027\u3092\u5b9f\u73fe\u3059\u308b\u9b54\u6cd5\u306e\u30c4\u30fc\u30eb\u306a\u306e\u3067\u3059\u3002\u6b21\u7ae0\u3067\u306f\u3001\u5b9f\u969b\u306bCython\u3092\u4f7f\u3063\u3066\u307f\u3066\u3001\u305d\u306e\u4f7f\u3044\u65b9\u3092\u8a73\u3057\u304f\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-4\">Cython\u306e\u4f7f\u3044\u65b9 \u2013 \u30b7\u30f3\u30d7\u30eb\u306a\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3067\u5b66\u3076<\/h2>\n\n\n\n<p>Cython\u3092\u4f7f\u3063\u3066Python\u306e\u30b3\u30fc\u30c9\u3092\u9ad8\u901f\u5316\u3059\u308b\u306b\u306f\u3001\u307e\u305aCython\u306e\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9\u3092\u7406\u89e3\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u3053\u3067\u306f\u3001\u30b7\u30f3\u30d7\u30eb\u306a\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u4ea4\u3048\u306a\u304c\u3089\u3001Cython\u3068Python\u306e\u9023\u643a\u65b9\u6cd5\u3001\u578b\u5ba3\u8a00\u3068cdef\u306e\u4f7f\u3044\u65b9\u3001\u305d\u3057\u3066Cython\u306e\u30b3\u30f3\u30d1\u30a4\u30eb\u65b9\u6cd5\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-5\">Cython\u3068Python\u306e\u9023\u643a\u65b9\u6cd5 \u2013 pxd\u30d5\u30a1\u30a4\u30eb\u3068pyx\u30d5\u30a1\u30a4\u30eb<\/h3>\n\n\n\n<p>Cython\u306e\u30b3\u30fc\u30c9\u306f\u3001.pyx\u3068\u3044\u3046\u62e1\u5f35\u5b50\u306e\u30d5\u30a1\u30a4\u30eb\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002\u307e\u305f\u3001Python\u306e\u578b\u60c5\u5831\u3092\u63d0\u4f9b\u3059\u308b\u305f\u3081\u306b\u3001.pxd\u3068\u3044\u3046\u62e1\u5f35\u5b50\u306e\u30d8\u30c3\u30c0\u30d5\u30a1\u30a4\u30eb\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002.pyx\u30d5\u30a1\u30a4\u30eb\u306b\u306f\u3001Cython\u306e\u5b9f\u88c5\u30b3\u30fc\u30c9\u3092\u8a18\u8ff0\u3057\u3001.pxd\u30d5\u30a1\u30a4\u30eb\u306b\u306f\u3001Python\u304b\u3089\u5229\u7528\u3059\u308b\u95a2\u6570\u3084\u30af\u30e9\u30b9\u306e\u5ba3\u8a00\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a.pxd\u30d5\u30a1\u30a4\u30eb\u3068.pyx\u30d5\u30a1\u30a4\u30eb\u3092\u7528\u610f\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># math_functions.pxd\ncdef int square(int x)<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># math_functions.pyx\ncdef int square(int x):\n    return x * x<\/pre>\n\n\n\n<p>\u3053\u3053\u3067\u306f\u3001<code>square<\/code>\u95a2\u6570\u3092Cython\u3067\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059\u3002.pxd\u30d5\u30a1\u30a4\u30eb\u3067\u95a2\u6570\u306e\u5ba3\u8a00\u3092\u884c\u3044\u3001.pyx\u30d5\u30a1\u30a4\u30eb\u3067\u5b9f\u969b\u306e\u5b9f\u88c5\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-6\">\u578b\u5ba3\u8a00\u3068cdef\u306e\u4f7f\u3044\u65b9 \u2013 \u9759\u7684\u578b\u4ed8\u3051\u306b\u3088\u308b\u9ad8\u901f\u5316<\/h3>\n\n\n\n<p>Cython\u3067\u306f\u3001\u5909\u6570\u3084\u95a2\u6570\u306e\u5f15\u6570\u30fb\u623b\u308a\u5024\u306b\u578b\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u3001\u9759\u7684\u578b\u4ed8\u3051\u3092\u5b9f\u73fe\u3057\u307e\u3059\u3002\u578b\u5ba3\u8a00\u306b\u306f\u3001<code>cdef<\/code>\u30ad\u30fc\u30ef\u30fc\u30c9\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001<code>cdef int x = 0<\/code>\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3059\u308b\u3053\u3068\u3067\u3001\u5909\u6570<code>x<\/code>\u3092int\u578b\u3068\u3057\u3066\u5ba3\u8a00\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u578b\u5ba3\u8a00\u3059\u308b\u3053\u3068\u3067\u3001Cython\u306fC\u30b3\u30fc\u30c9\u3078\u306e\u5909\u63db\u6642\u306b\u3088\u308a\u6700\u9069\u5316\u3055\u308c\u305f\u30b3\u30fc\u30c9\u3092\u751f\u6210\u3057\u3001\u9ad8\u901f\u5316\u3092\u5b9f\u73fe\u3057\u307e\u3059\u3002\u4e0a\u8a18\u306e<code>square<\/code>\u95a2\u6570\u3067\u306f\u3001\u5f15\u6570\u3068\u623b\u308a\u5024\u3092int\u578b\u3068\u3057\u3066\u5ba3\u8a00\u3057\u3066\u3044\u308b\u305f\u3081\u3001Cython\u306f\u52b9\u7387\u7684\u306aC\u30b3\u30fc\u30c9\u3092\u751f\u6210\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-7\">Cython\u306e\u30b3\u30f3\u30d1\u30a4\u30eb\u65b9\u6cd5 \u2013 distutils\/setuptools\u306e\u8a2d\u5b9a<\/h3>\n\n\n\n<p>Cython\u306e\u30b3\u30fc\u30c9\u3092\u30b3\u30f3\u30d1\u30a4\u30eb\u3059\u308b\u306b\u306f\u3001setuptools\u307e\u305f\u306fdistutils\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002setup.py\u30d5\u30a1\u30a4\u30eb\u306b\u30b3\u30f3\u30d1\u30a4\u30eb\u8a2d\u5b9a\u3092\u8a18\u8ff0\u3057\u3001\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u304b\u3089\u30b3\u30f3\u30d1\u30a4\u30eb\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># setup.py\nfrom setuptools import setup\nfrom Cython.Build import cythonize\n\nsetup(\n    name=\"math_functions\",\n    ext_modules=cythonize(\"math_functions.pyx\"),\n)<\/pre>\n\n\n\n<p>\u4e0a\u8a18\u306e\u3088\u3046\u306b\u3001setup.py\u30d5\u30a1\u30a4\u30eb\u3092\u7528\u610f\u3057\u3001<code>cythonize<\/code>\u95a2\u6570\u3092\u4f7f\u3063\u3066Cython\u306e\u30b3\u30fc\u30c9\u3092\u30b3\u30f3\u30d1\u30a4\u30eb\u3059\u308b\u8a2d\u5b9a\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u30b3\u30f3\u30d1\u30a4\u30eb\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u304b\u3089\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python setup.py build_ext --inplace<\/pre>\n\n\n\n<p>\u30b3\u30f3\u30d1\u30a4\u30eb\u306b\u3088\u308a\u3001.so\u307e\u305f\u306f.pyd\u3068\u3044\u3046\u62e1\u5f35\u5b50\u306e\u5171\u6709\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u751f\u6210\u3055\u308c\u3001Python\u304b\u3089\u5229\u7528\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u751f\u6210\u3055\u308c\u305f\u5171\u6709\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u3001\u6b21\u306e\u3088\u3046\u306bPython\u304b\u3089\u76f4\u63a5\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u3066\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import math_functions\n\nresult = math_functions.square(5)\nprint(result)  # 25<\/pre>\n\n\n\n<p>\u3053\u306e\u3088\u3046\u306b\u3001Cython\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001Python\u306e\u30b3\u30fc\u30c9\u3092\u5bb9\u6613\u306b\u9ad8\u901f\u5316\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u6b21\u7ae0\u3067\u306f\u3001Cython\u3092\u6a5f\u68b0\u5b66\u7fd2\u306e\u5206\u91ce\u306b\u9069\u7528\u3057\u3001NumPy\u3084Pandas\u306a\u3069\u306e\u4eba\u6c17\u30e9\u30a4\u30d6\u30e9\u30ea\u3068\u9023\u643a\u3059\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-8\">Cython\u306b\u3088\u308b\u6a5f\u68b0\u5b66\u7fd2\u306e\u9ad8\u901f\u5316\u30c6\u30af\u30cb\u30c3\u30af<\/h2>\n\n\n\n<p>Cython\u306f\u3001\u6a5f\u68b0\u5b66\u7fd2\u306e\u5206\u91ce\u3067\u3082\u5927\u304d\u306a\u5a01\u529b\u3092\u767a\u63ee\u3057\u307e\u3059\u3002NumPy\u3084Pandas\u306a\u3069\u306e\u30c7\u30fc\u30bf\u51e6\u7406\u30e9\u30a4\u30d6\u30e9\u30ea\u3001\u305d\u3057\u3066Scikit-learn\u306a\u3069\u306e\u6a5f\u68b0\u5b66\u7fd2\u30e9\u30a4\u30d6\u30e9\u30ea\u3068\u9023\u643a\u3059\u308b\u3053\u3068\u3067\u3001\u6a5f\u68b0\u5b66\u7fd2\u30d1\u30a4\u30d7\u30e9\u30a4\u30f3\u306e\u5404\u6bb5\u968e\u3067\u51e6\u7406\u901f\u5ea6\u3092\u5927\u5e45\u306b\u6539\u5584\u3067\u304d\u307e\u3059\u3002\u3053\u3053\u3067\u306f\u3001\u305d\u308c\u305e\u308c\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3068Cython\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u65b9\u6cd5\u3092\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-9\">NumPy\u3068Cython\u306e\u9023\u643a \u2013 \u9ad8\u901f\u306a\u30d9\u30af\u30c8\u30eb\/\u884c\u5217\u8a08\u7b97<\/h3>\n\n\n\n<p>NumPy\u306f\u3001Python\u306b\u304a\u3051\u308b\u6570\u5024\u8a08\u7b97\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u4ee3\u8868\u683c\u3067\u3059\u3002\u591a\u6b21\u5143\u914d\u5217\u3084\u884c\u5217\u8a08\u7b97\u306b\u7279\u5316\u3057\u3066\u304a\u308a\u3001\u6a5f\u68b0\u5b66\u7fd2\u306e\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u5b9f\u88c5\u3059\u308b\u4e0a\u3067\u6b20\u304b\u305b\u306a\u3044\u5b58\u5728\u3067\u3059\u3002Cython\u3092\u4f7f\u3063\u3066NumPy\u306e\u914d\u5217\u64cd\u4f5c\u3092\u9ad8\u901f\u5316\u3059\u308b\u3053\u3068\u3067\u3001\u6a5f\u68b0\u5b66\u7fd2\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u901f\u5ea6\u3092\u6539\u5584\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>NumPy\u306e\u914d\u5217\u3092Cython\u5185\u3067\u76f4\u63a5\u64cd\u4f5c\u3059\u308b\u306b\u306f\u3001<code>numpy.pxd<\/code>\u30d8\u30c3\u30c0\u30d5\u30a1\u30a4\u30eb\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u3001<code>numpy<\/code>\u578b\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u4ee5\u4e0b\u306f\u3001NumPy\u306e\u914d\u5217\u306e\u8981\u7d20\u3092\u5408\u8a08\u3059\u308bCython\u306e\u95a2\u6570\u306e\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># cython_numpy_example.pyx\ncimport numpy as np\n\ndef sum_array(np.ndarray[np.int64_t] arr):\n    cdef int i, n = arr.shape[0], total = 0\n    for i in range(n):\n        total += arr[i]\n    return total<\/pre>\n\n\n\n<p>\u3053\u306e\u95a2\u6570\u3067\u306f\u3001<code>numpy.ndarray<\/code>\u578b\u306e\u5f15\u6570<code>arr<\/code>\u3092\u53d7\u3051\u53d6\u308a\u3001\u305d\u306e\u8981\u7d20\u3092\u5408\u8a08\u3057\u3066\u8fd4\u3057\u307e\u3059\u3002Cython\u306e\u578b\u5ba3\u8a00\u306b\u3088\u308a\u3001NumPy\u306e\u914d\u5217\u306b\u76f4\u63a5\u30a2\u30af\u30bb\u30b9\u3057\u3001\u9ad8\u901f\u306b\u30eb\u30fc\u30d7\u51e6\u7406\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-10\">Pandas\u3068Cython\u306e\u9023\u643a \u2013 \u30c7\u30fc\u30bf\u524d\u51e6\u7406\u306e\u9ad8\u901f\u5316<\/h3>\n\n\n\n<p>Pandas\u306f\u3001\u30c7\u30fc\u30bf\u89e3\u6790\u306b\u7279\u5316\u3057\u305fPython\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u3092\u4e2d\u5fc3\u3068\u3057\u305f\u30c7\u30fc\u30bf\u64cd\u4f5c\u3092\u63d0\u4f9b\u3057\u3001\u6a5f\u68b0\u5b66\u7fd2\u306b\u304a\u3051\u308b\u30c7\u30fc\u30bf\u306e\u524d\u51e6\u7406\u306b\u3088\u304f\u4f7f\u308f\u308c\u307e\u3059\u3002Cython\u3067Pandas\u306e\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u64cd\u4f5c\u3092\u9ad8\u901f\u5316\u3059\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u524d\u51e6\u7406\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u6539\u5584\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>Pandas\u306e\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u3092Cython\u5185\u3067\u76f4\u63a5\u64cd\u4f5c\u3059\u308b\u306b\u306f\u3001<code>pandas.pxd<\/code>\u30d8\u30c3\u30c0\u30d5\u30a1\u30a4\u30eb\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u3001<code>pandas<\/code>\u578b\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u4ee5\u4e0b\u306f\u3001Pandas\u306e\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u306e\u7279\u5b9a\u306e\u5217\u306e\u5408\u8a08\u3092\u8a08\u7b97\u3059\u308bCython\u306e\u95a2\u6570\u306e\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># cython_pandas_example.pyx\ncimport pandas as pd\n\ndef sum_column(pd.DataFrame df, str col_name):\n    cdef int i, n = len(df)\n    cdef double total = 0\n    for i in range(n):\n        total += df[col_name][i]\n    return total<\/pre>\n\n\n\n<p>\u3053\u306e\u95a2\u6570\u3067\u306f\u3001<code>pandas.DataFrame<\/code>\u578b\u306e\u5f15\u6570<code>df<\/code>\u3068\u3001\u5217\u540d\u3092\u8868\u3059\u6587\u5b57\u5217<code>col_name<\/code>\u3092\u53d7\u3051\u53d6\u308a\u3001\u6307\u5b9a\u3055\u308c\u305f\u5217\u306e\u8981\u7d20\u3092\u5408\u8a08\u3057\u3066\u8fd4\u3057\u307e\u3059\u3002Cython\u306e\u578b\u5ba3\u8a00\u306b\u3088\u308a\u3001\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u306e\u8981\u7d20\u306b\u76f4\u63a5\u30a2\u30af\u30bb\u30b9\u3057\u3001\u9ad8\u901f\u306b\u8a08\u7b97\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-11\">Scikit-learn\u3068Cython\u306e\u9023\u643a \u2013 \u30e2\u30c7\u30eb\u306e\u5b66\u7fd2\u3068\u4e88\u6e2c\u306e\u9ad8\u901f\u5316<\/h3>\n\n\n\n<p>Scikit-learn\u306f\u3001\u6a5f\u68b0\u5b66\u7fd2\u306e\u305f\u3081\u306ePython\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002\u69d8\u3005\u306a\u6a5f\u68b0\u5b66\u7fd2\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u63d0\u4f9b\u3057\u3066\u304a\u308a\u3001\u7c21\u5358\u306b\u6a5f\u68b0\u5b66\u7fd2\u30e2\u30c7\u30eb\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002Cython\u3067Scikit-learn\u306e\u6a5f\u68b0\u5b66\u7fd2\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u9ad8\u901f\u5316\u3059\u308b\u3053\u3068\u3067\u3001\u30e2\u30c7\u30eb\u306e\u5b66\u7fd2\u3068\u4e88\u6e2c\u306e\u901f\u5ea6\u3092\u6539\u5584\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>Scikit-learn\u306e\u6a5f\u68b0\u5b66\u7fd2\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092Cython\u5185\u3067\u76f4\u63a5\u5229\u7528\u3059\u308b\u306b\u306f\u3001\u5404\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u306e\u5185\u90e8\u5b9f\u88c5\u3092\u7406\u89e3\u3057\u3001Cython\u3067\u518d\u5b9f\u88c5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306f\u3001K-Nearest Neighbors\uff08KNN\uff09\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092Cython\u3067\u5b9f\u88c5\u3057\u305f\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># cython_knn_example.pyx\nimport numpy as np\ncimport numpy as np\n\ndef euclidean_distance(np.ndarray[np.float64_t] a, np.ndarray[np.float64_t] b):\n    cdef int i, n = a.shape[0]\n    cdef double dist = 0\n    for i in range(n):\n        dist += (a[i] - b[i]) ** 2\n    return np.sqrt(dist)\n\ndef knn_predict(np.ndarray[np.float64_t] X_train, np.ndarray[np.int64_t] y_train, np.ndarray[np.float64_t] X_test, int k):\n    cdef int i, j, n_train = X_train.shape[0], n_test = X_test.shape[0]\n    cdef np.ndarray[np.int64_t] y_pred = np.empty(n_test, dtype=np.int64)\n\n    for i in range(n_test):\n        distances = [euclidean_distance(X_test[i], X_train[j]) for j in range(n_train)]\n        nearest_indices = np.argsort(distances)[:k]\n        y_pred[i] = np.bincount(y_train[nearest_indices]).argmax()\n\n    return y_pred<\/pre>\n\n\n\n<p>\u3053\u306e\u4f8b\u3067\u306f\u3001\u30e6\u30fc\u30af\u30ea\u30c3\u30c9\u8ddd\u96e2\u3092\u8a08\u7b97\u3059\u308b<code>euclidean_distance<\/code>\u95a2\u6570\u3068\u3001KNN\u306e\u30e9\u30d9\u30eb\u4e88\u6e2c\u3092\u884c\u3046<code>knn_predict<\/code>\u95a2\u6570\u3092\u5b9a\u7fa9\u3057\u3066\u3044\u307e\u3059\u3002<code>euclidean_distance<\/code>\u95a2\u6570\u3067\u306f\u30012\u3064\u306e\u30d9\u30af\u30c8\u30eb<code>a<\/code>\u3068<code>b<\/code>\u306e\u9593\u306e\u30e6\u30fc\u30af\u30ea\u30c3\u30c9\u8ddd\u96e2\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002<code>knn_predict<\/code>\u95a2\u6570\u3067\u306f\u3001\u8a13\u7df4\u30c7\u30fc\u30bf<code>X_train<\/code>\u3068<code>y_train<\/code>\u3001\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf<code>X_test<\/code>\u3001\u304a\u3088\u3073\u8fd1\u508d\u306e\u6570<code>k<\/code>\u3092\u53d7\u3051\u53d6\u308a\u3001\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306b\u5bfe\u3059\u308b\u30e9\u30d9\u30eb\u306e\u4e88\u6e2c\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u308c\u3089\u306e\u95a2\u6570\u3067\u306f\u3001NumPy\u306e\u914d\u5217\u306b\u5bfe\u3057\u3066Cython\u306e\u578b\u5ba3\u8a00\u3092\u884c\u3046\u3053\u3068\u3067\u3001\u9ad8\u901f\u306a\u30eb\u30fc\u30d7\u51e6\u7406\u3092\u5b9f\u73fe\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u3088\u3046\u306b\u3001Scikit-learn\u306e\u6a5f\u68b0\u5b66\u7fd2\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092Cython\u3067\u518d\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3067\u3001\u30e2\u30c7\u30eb\u306e\u5b66\u7fd2\u3068\u4e88\u6e2c\u306e\u901f\u5ea6\u3092\u5927\u5e45\u306b\u6539\u5584\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u306e\u3088\u3046\u306b\u3001NumPy\u3001Pandas\u3001Scikit-learn\u3068Cython\u3092\u9023\u643a\u3055\u305b\u308b\u3053\u3068\u3067\u3001\u6a5f\u68b0\u5b66\u7fd2\u30d1\u30a4\u30d7\u30e9\u30a4\u30f3\u306e\u30dc\u30c8\u30eb\u30cd\u30c3\u30af\u3092\u89e3\u6d88\u3057\u3001\u5168\u4f53\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067\u3059\u3002\u6b21\u7ae0\u3067\u306f\u3001Cython\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\u306e\u30b3\u30c4\u306b\u3064\u3044\u3066\u3001\u3055\u3089\u306b\u8a73\u3057\u304f\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-12\">Cython\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\u306e\u30b3\u30c4<\/h2>\n\n\n\n<p>Cython\u3092\u4f7f\u3063\u3066Python\u306e\u30b3\u30fc\u30c9\u3092\u9ad8\u901f\u5316\u3059\u308b\u969b\u306b\u306f\u3001\u3044\u304f\u3064\u304b\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\u306e\u30b3\u30c4\u3092\u62bc\u3055\u3048\u3066\u304a\u304f\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002\u3053\u3053\u3067\u306f\u3001\u30e1\u30e2\u30ea\u7ba1\u7406\u306e\u6700\u9069\u5316\u3001\u30d0\u30a6\u30f3\u30c9\u30c1\u30a7\u30c3\u30af\u306e\u524a\u9664\u3001\u4e26\u5217\u51e6\u7406\u306e\u6d3b\u7528\u3068\u3044\u3063\u305f\u6280\u8853\u306b\u3064\u3044\u3066\u3001\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u4ea4\u3048\u306a\u304c\u3089\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-13\">\u30e1\u30e2\u30ea\u7ba1\u7406\u306e\u6700\u9069\u5316 \u2013 \u7121\u99c4\u306a\u30e1\u30e2\u30ea\u30b3\u30d4\u30fc\u3092\u524a\u6e1b<\/h3>\n\n\n\n<p>Cython\u3067\u306f\u3001Python\u306e\u30ac\u30d9\u30fc\u30b8\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306b\u983c\u3089\u305a\u3001\u624b\u52d5\u3067\u30e1\u30e2\u30ea\u7ba1\u7406\u3092\u884c\u3046\u3053\u3068\u3067\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u6539\u5584\u3067\u304d\u307e\u3059\u3002<code>cdef<\/code>\u3092\u4f7f\u3063\u3066\u9759\u7684\u306b\u30e1\u30e2\u30ea\u3092\u78ba\u4fdd\u3057\u3001\u4e0d\u8981\u306b\u306a\u3063\u305f\u30e1\u30e2\u30ea\u306f<code>free()<\/code>\u3067\u89e3\u653e\u3057\u307e\u3059\u3002\u307e\u305f\u3001Python\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u76f4\u63a5\u64cd\u4f5c\u3059\u308b\u4ee3\u308f\u308a\u306b\u3001C\u306e\u578b\u3092\u4f7f\u3046\u3053\u3068\u3067\u30e1\u30e2\u30ea\u4f7f\u7528\u91cf\u3092\u524a\u6e1b\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u306f\u3001\u30e1\u30e2\u30ea\u7ba1\u7406\u3092\u6700\u9069\u5316\u3057\u305f\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from libc.stdlib cimport malloc, free\n\ndef sum_array(int[:] arr):\n    cdef int i, n = arr.shape[0], total = 0\n    cdef int* buffer = &lt;int*&gt;malloc(n * sizeof(int))\n\n    try:\n        for i in range(n):\n            buffer[i] = arr[i]\n            total += buffer[i]\n    finally:\n        free(buffer)\n\n    return total<\/pre>\n\n\n\n<p>\u3053\u306e\u4f8b\u3067\u306f\u3001<code>malloc()<\/code>\u3092\u4f7f\u3063\u3066\u6574\u6570\u578b\u306e\u914d\u5217<code>buffer<\/code>\u3092\u52d5\u7684\u306b\u78ba\u4fdd\u3057\u3001<code>arr<\/code>\u306e\u8981\u7d20\u3092\u30b3\u30d4\u30fc\u3057\u3066\u3044\u307e\u3059\u3002<code>buffer<\/code>\u3092\u4f7f\u3063\u3066\u5408\u8a08\u3092\u8a08\u7b97\u3059\u308b\u3053\u3068\u3067\u3001Python\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u76f4\u63a5\u64cd\u4f5c\u3059\u308b\u3088\u308a\u3082\u30e1\u30e2\u30ea\u52b9\u7387\u304c\u826f\u304f\u306a\u308a\u307e\u3059\u3002\u6700\u5f8c\u306b\u3001<code>free()<\/code>\u3092\u4f7f\u3063\u3066<code>buffer<\/code>\u306e\u30e1\u30e2\u30ea\u3092\u89e3\u653e\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-14\">\u30d0\u30a6\u30f3\u30c9\u30c1\u30a7\u30c3\u30af\u306e\u524a\u9664 \u2013 \u3055\u3089\u306a\u308b\u9ad8\u901f\u5316\u3092\u72d9\u3046<\/h3>\n\n\n\n<p>Cython\u306e\u30eb\u30fc\u30d7\u51e6\u7406\u3067\u306f\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u30d0\u30a6\u30f3\u30c9\u30c1\u30a7\u30c3\u30af\uff08index\u306e\u7bc4\u56f2\u30c1\u30a7\u30c3\u30af\uff09\u304c\u884c\u308f\u308c\u307e\u3059\u304c\u3001\u3053\u308c\u304c\u5b9f\u884c\u901f\u5ea6\u306e\u4f4e\u4e0b\u306b\u3064\u306a\u304c\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002<code>cython<\/code>\u30c7\u30a3\u30ec\u30af\u30c6\u30a3\u30d6\u3092\u4f7f\u3063\u3066\u30d0\u30a6\u30f3\u30c9\u30c1\u30a7\u30c3\u30af\u3092\u7121\u52b9\u5316\u3059\u308b\u3053\u3068\u3067\u3001\u3055\u3089\u306a\u308b\u9ad8\u901f\u5316\u304c\u53ef\u80fd\u3067\u3059\u3002\u305f\u3060\u3057\u3001\u30d0\u30a6\u30f3\u30c9\u30c1\u30a7\u30c3\u30af\u3092\u7121\u52b9\u5316\u3059\u308b\u5834\u5408\u306f\u3001index\u306e\u7bc4\u56f2\u306b\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u306f\u3001\u30d0\u30a6\u30f3\u30c9\u30c1\u30a7\u30c3\u30af\u3092\u7121\u52b9\u5316\u3057\u305f\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#cython: boundscheck=False\ndef sum_array(int[:] arr):\n    cdef int i, n = arr.shape[0], total = 0\n    for i in range(n):\n        total += arr[i]\n    return total<\/pre>\n\n\n\n<p>\u3053\u306e\u4f8b\u3067\u306f\u3001<code>#cython: boundscheck=False<\/code>\u30c7\u30a3\u30ec\u30af\u30c6\u30a3\u30d6\u3092\u4f7f\u3063\u3066\u30d0\u30a6\u30f3\u30c9\u30c1\u30a7\u30c3\u30af\u3092\u7121\u52b9\u5316\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u30eb\u30fc\u30d7\u51e6\u7406\u306e\u5b9f\u884c\u901f\u5ea6\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002\u305f\u3060\u3057\u3001<code>arr<\/code>\u306eindex\u304c\u7bc4\u56f2\u5916\u306b\u30a2\u30af\u30bb\u30b9\u3057\u306a\u3044\u3088\u3046\u306b\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-15\">\u4e26\u5217\u51e6\u7406\u306e\u6d3b\u7528 \u2013 \u30de\u30eb\u30c1\u30b3\u30a2CPU\u306e\u6027\u80fd\u3092\u5f15\u304d\u51fa\u3059<\/h3>\n\n\n\n<p>Cython\u3067\u306f\u3001OpenMP\u3084multiprocessing\u3092\u4f7f\u3063\u305f\u4e26\u5217\u51e6\u7406\u306b\u3088\u308a\u3001\u30de\u30eb\u30c1\u30b3\u30a2CPU\u306e\u6027\u80fd\u3092\u5f15\u304d\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<code>prange()<\/code>\u95a2\u6570\u3092\u4f7f\u3063\u3066\u30eb\u30fc\u30d7\u51e6\u7406\u3092\u4e26\u5217\u5316\u3067\u304d\u307e\u3059\u3002\u4e26\u5217\u5316\u3059\u308b\u969b\u306f\u3001\u30c7\u30fc\u30bf\u306e\u4f9d\u5b58\u95a2\u4fc2\u3084\u30b9\u30ec\u30c3\u30c9\u9593\u306e\u540c\u671f\u306b\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u306f\u3001OpenMP\u3092\u4f7f\u3063\u3066\u4e26\u5217\u5316\u3057\u305f\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from cython.parallel import prange\n\ndef parallel_sum(int[:] arr):\n    cdef int i, total = 0\n    cdef int n = arr.shape[0]\n\n    for i in prange(n, nogil=True):\n        total += arr[i]\n\n    return total<\/pre>\n\n\n\n<p>\u3053\u306e\u4f8b\u3067\u306f\u3001<code>prange()<\/code>\u95a2\u6570\u3092\u4f7f\u3063\u3066\u30eb\u30fc\u30d7\u51e6\u7406\u3092\u4e26\u5217\u5316\u3057\u3066\u3044\u307e\u3059\u3002<code>nogil=True<\/code>\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u3001Global Interpreter Lock\uff08GIL\uff09\u3092\u89e3\u653e\u3057\u3001\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9\u3067\u306e\u4e26\u5217\u5b9f\u884c\u3092\u53ef\u80fd\u306b\u3057\u3066\u3044\u307e\u3059\u3002\u305f\u3060\u3057\u3001<code>total<\/code>\u5909\u6570\u3078\u306e\u30a2\u30af\u30bb\u30b9\u306b\u306f\u540c\u671f\u304c\u5fc5\u8981\u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u306e\u3088\u3046\u306b\u3001\u30e1\u30e2\u30ea\u7ba1\u7406\u306e\u6700\u9069\u5316\u3001\u30d0\u30a6\u30f3\u30c9\u30c1\u30a7\u30c3\u30af\u306e\u524a\u9664\u3001\u4e26\u5217\u51e6\u7406\u306e\u6d3b\u7528\u3068\u3044\u3063\u305f\u30c6\u30af\u30cb\u30c3\u30af\u3092\u99c6\u4f7f\u3059\u308b\u3053\u3068\u3067\u3001Cython\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u3055\u3089\u306b\u5f15\u304d\u4e0a\u3052\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u6b21\u7ae0\u3067\u306f\u3001\u5b9f\u969b\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u306eCython\u6d3b\u7528\u4e8b\u4f8b\u3092\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-16\">Cython\u306e\u6d3b\u7528\u4e8b\u4f8b \u2013 \u5b9f\u969b\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u304b\u3089\u5b66\u3076<\/h2>\n\n\n\n<p>Cython\u306f\u3001\u69d8\u3005\u306a\u5206\u91ce\u306ePython\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u6d3b\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u3053\u3053\u3067\u306f\u3001\u30c7\u30a3\u30fc\u30d7\u30e9\u30fc\u30cb\u30f3\u30b0\u3001\u30d3\u30c3\u30b0\u30c7\u30fc\u30bf\u5206\u6790\u3001Web\u958b\u767a\u3068\u3044\u3063\u305f\u5206\u91ce\u3067\u306e\u4e8b\u4f8b\u3092\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-17\">Cython\u00d7\u30c7\u30a3\u30fc\u30d7\u30e9\u30fc\u30cb\u30f3\u30b0 \u2013 TensorFlow\u3084PyTorch\u306e\u9ad8\u901f\u5316<\/h3>\n\n\n\n<p>TensorFlow\u3084PyTorch\u306a\u3069\u306e\u30c7\u30a3\u30fc\u30d7\u30e9\u30fc\u30cb\u30f3\u30b0\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u306f\u3001\u8a08\u7b97\u306e\u30dc\u30c8\u30eb\u30cd\u30c3\u30af\u3068\u306a\u308b\u90e8\u5206\u3092Cython\u3067\u9ad8\u901f\u5316\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u30ab\u30b9\u30bf\u30e0\u640d\u5931\u95a2\u6570\u3084\u30c7\u30fc\u30bf\u524d\u51e6\u7406\u306e\u30b3\u30fc\u30c9\u3092Cython\u3067\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3067\u3001\u30e2\u30c7\u30eb\u306e\u5b66\u7fd2\u901f\u5ea6\u3092\u6539\u5584\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u306f\u3001PyTorch\u306e\u30ab\u30b9\u30bf\u30e0\u640d\u5931\u95a2\u6570\u3092Cython\u3067\u5b9f\u88c5\u3057\u305f\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># custom_loss.pyx\nimport torch\ncimport numpy as np\n\ndef custom_loss_function(torch.Tensor y_true, torch.Tensor y_pred):\n    cdef int i, n = y_true.size(0)\n    cdef float loss = 0\n    cdef np.ndarray[np.float32_t] y_true_np = y_true.numpy()\n    cdef np.ndarray[np.float32_t] y_pred_np = y_pred.numpy()\n\n    for i in range(n):\n        loss += (y_true_np[i] - y_pred_np[i]) ** 2\n\n    return torch.tensor(loss \/ n)<\/pre>\n\n\n\n<p>\u3053\u306e\u4f8b\u3067\u306f\u3001<code>y_true<\/code>\u3068<code>y_pred<\/code>\u306e\u8981\u7d20\u3054\u3068\u306e\u5dee\u306e\u4e8c\u4e57\u548c\u3092\u8a08\u7b97\u3057\u3001\u5e73\u5747\u5316\u3059\u308b\u3053\u3068\u3067\u640d\u5931\u3092\u8a08\u7b97\u3057\u3066\u3044\u307e\u3059\u3002Cython\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001NumPy\u306e\u914d\u5217\u306b\u76f4\u63a5\u30a2\u30af\u30bb\u30b9\u3057\u3001\u9ad8\u901f\u306b\u30eb\u30fc\u30d7\u51e6\u7406\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-18\">Cython\u00d7\u30d3\u30c3\u30b0\u30c7\u30fc\u30bf\u5206\u6790 \u2013 PySpark\/Dask\u306e\u51e6\u7406\u3092\u9ad8\u901f\u5316<\/h3>\n\n\n\n<p>PySpark\u3001Dask\u3001Pandas\u3001NumPy\u306a\u3069\u306e\u30c7\u30fc\u30bf\u51e6\u7406\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u306f\u3001Cython\u3092\u4f7f\u3063\u3066\u30dc\u30c8\u30eb\u30cd\u30c3\u30af\u90e8\u5206\u3092\u9ad8\u901f\u5316\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001PySpark\u4e0a\u3067Cython\u306eUDF\u3092\u5b9a\u7fa9\u3057\u3066\u3001\u30c7\u30fc\u30bf\u51e6\u7406\u3092\u9ad8\u901f\u5316\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u306f\u3001PySpark UDF\u3092Cython\u3067\u5b9f\u88c5\u3057\u305f\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from pyspark.sql.functions import udf\nfrom pyspark.sql.types import FloatType\n\n# Cython\u3067\u5b9f\u88c5\u3057\u305fUDF\ndef cython_udf(x):\n    return x * 2\n\n# UDF\u306e\u767b\u9332\ncython_udf_spark = udf(cython_udf, FloatType())\n\n# UDF\u306e\u9069\u7528\ndf = df.withColumn(\"result\", cython_udf_spark(\"value\"))<\/pre>\n\n\n\n<p>\u3053\u306e\u4f8b\u3067\u306f\u3001<code>cython_udf<\/code>\u95a2\u6570\u3092\u5b9a\u7fa9\u3057\u3001\u5024\u30922\u500d\u306b\u3059\u308bUDF\u3092\u4f5c\u6210\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306eUDF\u3092PySpark\u306e\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u306b\u9069\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u9ad8\u901f\u306a\u30c7\u30fc\u30bf\u51e6\u7406\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-19\">Cython\u00d7Web\u958b\u767a \u2013 \u30dc\u30c8\u30eb\u30cd\u30c3\u30af\u3068\u306a\u308b\u51e6\u7406\u306e\u9ad8\u901f\u5316<\/h3>\n\n\n\n<p>Web\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3084ORM\u306a\u3069\u3067\u306f\u3001Cython\u3092\u4f7f\u3063\u3066\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u30dc\u30c8\u30eb\u30cd\u30c3\u30af\u3092\u89e3\u6d88\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001Django\u306e\u30af\u30a8\u30ea\u30bb\u30c3\u30c8\u3084Flask\u306e\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u51e6\u7406\u3092Cython\u3067\u9ad8\u901f\u5316\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u306f\u3001Django\u306e\u30af\u30a8\u30ea\u30bb\u30c3\u30c8\u3092\u6700\u9069\u5316\u3059\u308b\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from django.db import models\nfrom django.db.models.query import QuerySet\n\nclass CythonQuerySet(QuerySet):\n    def cython_filter(self, *args, **kwargs):\n        # Cython\u3067\u6700\u9069\u5316\u3057\u305f\u30d5\u30a3\u30eb\u30bf\u30ea\u30f3\u30b0\u51e6\u7406\n        pass\n\nclass MyModel(models.Model):\n    objects = CythonQuerySet.as_manager()\n\n    # \u30e2\u30c7\u30eb\u306e\u5b9a\u7fa9<\/pre>\n\n\n\n<p>\u3053\u306e\u4f8b\u3067\u306f\u3001<code>CythonQuerySet<\/code>\u30af\u30e9\u30b9\u3092\u5b9a\u7fa9\u3057\u3001<code>cython_filter<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u306f\u3001Cython\u3067\u6700\u9069\u5316\u3057\u305f\u30d5\u30a3\u30eb\u30bf\u30ea\u30f3\u30b0\u51e6\u7406\u3092\u884c\u3044\u307e\u3059\u3002<code>MyModel<\/code>\u30af\u30e9\u30b9\u306e<code>objects<\/code>\u30de\u30cd\u30fc\u30b8\u30e3\u30fc\u306b<code>CythonQuerySet<\/code>\u3092\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u3067\u3001\u6700\u9069\u5316\u3055\u308c\u305f\u30af\u30a8\u30ea\u30bb\u30c3\u30c8\u3092\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u306e\u3088\u3046\u306b\u3001\u69d8\u3005\u306a\u5206\u91ce\u3067Cython\u3092\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u3001Python\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u5927\u5e45\u306b\u6539\u5584\u3067\u304d\u307e\u3059\u3002\u6b21\u7ae0\u3067\u306f\u3001Cython\u306e\u4eca\u5f8c\u306e\u5c55\u671b\u306b\u3064\u3044\u3066\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-20\">Cython\u306e\u3053\u308c\u304b\u3089 \u2013 \u6700\u65b0\u306e\u958b\u767a\u52d5\u5411\u3068\u4eca\u5f8c\u306e\u5c55\u671b<\/h2>\n\n\n\n<p>Cython\u306f\u3001\u4eca\u3082\u6d3b\u767a\u306b\u958b\u767a\u304c\u7d9a\u3051\u3089\u308c\u3066\u304a\u308a\u3001\u4eca\u5f8c\u3082Python\u306e\u9ad8\u901f\u5316\u306b\u5927\u304d\u304f\u8ca2\u732e\u3057\u3066\u3044\u304f\u3053\u3068\u304c\u671f\u5f85\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u3053\u3053\u3067\u306f\u3001Cython\u306e\u6700\u65b0\u306e\u958b\u767a\u52d5\u5411\u3068\u4eca\u5f8c\u306e\u5c55\u671b\u306b\u3064\u3044\u3066\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-21\">Cython 3.0\u306e\u65b0\u6a5f\u80fd\u3068\u6539\u5584\u70b9<\/h3>\n\n\n\n<p>Cython 3.0\u3067\u306f\u3001Python 3.9\u3068\u306e\u4e92\u63db\u6027\u304c\u5411\u4e0a\u3057\u3001\u65b0\u3057\u3044\u8a00\u8a9e\u6a5f\u80fd\u304c\u30b5\u30dd\u30fc\u30c8\u3055\u308c\u308b\u305d\u3046\u3067\u3059\u3002\u307e\u305f\u3001PEP 484\u306b\u57fa\u3065\u304f\u578b\u30d2\u30f3\u30c8\u306e\u30b5\u30dd\u30fc\u30c8\u304c\u5f37\u5316\u3055\u308c\u3001\u578b\u30c1\u30a7\u30c3\u30af\u3068\u30b3\u30f3\u30d1\u30a4\u30e9\u6700\u9069\u5316\u304c\u6539\u5584\u3055\u308c\u307e\u3059\u3002\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u6539\u5584\u3084\u30d0\u30b0\u4fee\u6b63\u306a\u3069\u3001\u591a\u304f\u306e\u6539\u5584\u304c\u884c\u308f\u308c\u308b\u898b\u8fbc\u307f\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u306f\u3001\u578b\u30d2\u30f3\u30c8\u3092\u4f7f\u3063\u305fCython\u30b3\u30fc\u30c9\u306e\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def sum_squares(int n) -&gt; int:\n    cdef int i, total = 0\n    for i in range(n):\n        total += i * i\n    return total<\/pre>\n\n\n\n<p>\u3053\u306e\u4f8b\u3067\u306f\u3001\u95a2\u6570\u306e\u5f15\u6570\u3068\u623b\u308a\u5024\u306b\u578b\u30d2\u30f3\u30c8\u3092\u4ed8\u3051\u3066\u3044\u307e\u3059\u3002Cython 3.0\u3067\u306f\u3001\u3053\u306e\u3088\u3046\u306a\u578b\u30d2\u30f3\u30c8\u3092\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u3088\u308a\u52b9\u7387\u7684\u306a\u30b3\u30fc\u30c9\u306e\u6700\u9069\u5316\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-22\">Python\u3068Cython\u306e\u57a3\u6839\u304c\u3055\u3089\u306b\u4f4e\u304f\u306a\u308b\uff1fPythran\u767b\u5834<\/h3>\n\n\n\n<p>Pythran\u306f\u3001Python\u30b3\u30fc\u30c9\u3092\u305d\u306e\u307e\u307e\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u3066\u9ad8\u901f\u5316\u3059\u308b\u305f\u3081\u306e\u30c4\u30fc\u30eb\u3067\u3059\u3002NumPy\u3084SciPy\u3092\u4f7f\u3063\u305f\u30b3\u30fc\u30c9\u3092\u3001Cython\u3092\u4f7f\u308f\u305a\u306b\u9ad8\u901f\u5316\u3067\u304d\u307e\u3059\u3002Python\u3068Cython\u306e\u57a3\u6839\u304c\u3055\u3089\u306b\u4f4e\u304f\u306a\u308a\u3001Python\u30e6\u30fc\u30b6\u30fc\u304c\u3088\u308a\u7c21\u5358\u306b\u9ad8\u901f\u5316\u3092\u5b9f\u73fe\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u306f\u3001Pythran\u3092\u4f7f\u3063\u305f\u30b3\u30fc\u30c9\u306e\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#pythran export sum_squares(int)\nimport numpy as np\n\ndef sum_squares(n):\n    return np.sum(np.arange(n) ** 2)<\/pre>\n\n\n\n<p>\u3053\u306e\u4f8b\u3067\u306f\u3001<code>#pythran export<\/code>\u30c7\u30a3\u30ec\u30af\u30c6\u30a3\u30d6\u3092\u4f7f\u3063\u3066\u3001<code>sum_squares<\/code>\u95a2\u6570\u3092Pythran\u3067\u30b3\u30f3\u30d1\u30a4\u30eb\u3059\u308b\u3088\u3046\u306b\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002Pythran\u306f\u3001\u3053\u306e\u30b3\u30fc\u30c9\u3092\u81ea\u52d5\u7684\u306b\u6700\u9069\u5316\u3057\u3001\u9ad8\u901f\u306a\u5b9f\u884c\u53ef\u80fd\u30b3\u30fc\u30c9\u3092\u751f\u6210\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-23\">\u4ed6\u8a00\u8a9e\u3068\u306e\u9023\u643a \u2013 Rust\/Julia\/Go\u3068\u306e\u6bd4\u8f03<\/h3>\n\n\n\n<p>Rust\u3084Julia\u3001Go\u306a\u3069\u3001\u4ed6\u306e\u9ad8\u901f\u306a\u8a00\u8a9e\u3068\u306e\u9023\u643a\u3082\u9032\u3093\u3067\u3044\u307e\u3059\u3002\u305d\u308c\u305e\u308c\u306e\u8a00\u8a9e\u306e\u9577\u6240\u3092\u6d3b\u304b\u3057\u3064\u3064\u3001Cython\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001\u3088\u308a\u9ad8\u5ea6\u306a\u9ad8\u901f\u5316\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002\u8a00\u8a9e\u9593\u306e\u76f8\u4e92\u904b\u7528\u6027\u304c\u5411\u4e0a\u3057\u3001\u69d8\u3005\u306a\u8a00\u8a9e\u3092\u7d44\u307f\u5408\u308f\u305b\u305f\u30cf\u30a4\u30d6\u30ea\u30c3\u30c9\u306a\u958b\u767a\u304c\u5897\u3048\u3066\u3044\u304f\u3068\u4e88\u60f3\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u306f\u3001Rust\u3068Cython\u3092\u9023\u643a\u3055\u305b\u305f\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ rust_extension.rs\n#[no_mangle]\npub extern \"C\" fn rust_function(x: i32, y: i32) -&gt; i32 {\n    x + y\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># cython_module.pyx\ncdef extern from \"rust_extension.rs\":\n    int rust_function(int x, int y)\n\ndef cython_function(int x, int y):\n    return rust_function(x, y)<\/pre>\n\n\n\n<p>\u3053\u306e\u4f8b\u3067\u306f\u3001Rust\u3067\u5b9f\u88c5\u3055\u308c\u305f<code>rust_function<\/code>\u3092Cython\u304b\u3089\u547c\u3073\u51fa\u3057\u3066\u3044\u307e\u3059\u3002<code>cdef extern from<\/code>\u3092\u4f7f\u3063\u3066Rust\u306e\u95a2\u6570\u3092\u5ba3\u8a00\u3057\u3001Cython\u5185\u3067\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u3088\u3046\u306b\u3001\u8a00\u8a9e\u9593\u306e\u9023\u643a\u3092\u5f37\u5316\u3059\u308b\u3053\u3068\u3067\u3001\u3088\u308a\u9ad8\u5ea6\u306a\u9ad8\u901f\u5316\u304c\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>Cython\u306f\u3001\u4eca\u5f8c\u3082Python\u306e\u9ad8\u901f\u5316\u306b\u6b20\u304b\u305b\u306a\u3044\u30c4\u30fc\u30eb\u3067\u3042\u308a\u7d9a\u3051\u308b\u3067\u3057\u3087\u3046\u3002Cython\u81ea\u4f53\u306e\u9032\u5316\u3068\u3001\u4ed6\u8a00\u8a9e\u3068\u306e\u9023\u643a\u306b\u3088\u3063\u3066\u3001Python\u306e\u53ef\u80fd\u6027\u304c\u3055\u3089\u306b\u5e83\u304c\u3063\u3066\u3044\u304f\u3053\u3068\u304c\u671f\u5f85\u3055\u308c\u307e\u3059\u3002\u6b21\u7ae0\u3067\u306f\u3001\u3053\u308c\u307e\u3067\u306e\u5185\u5bb9\u3092\u632f\u308a\u8fd4\u308a\u3001Cython\u30de\u30b9\u30bf\u30fc\u3078\u306e\u7b2c\u4e00\u6b69\u3092\u8e0f\u307f\u51fa\u3059\u305f\u3081\u306e\u30a2\u30c9\u30d0\u30a4\u30b9\u3092\u307e\u3068\u3081\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-24\">\u307e\u3068\u3081 \u2013 Cython\u30de\u30b9\u30bf\u30fc\u3078\u306e\u7b2c\u4e00\u6b69<\/h2>\n\n\n\n<p>\u672c\u8a18\u4e8b\u3067\u306f\u3001Cython\u306e\u57fa\u672c\u6982\u5ff5\u304b\u3089\u5fdc\u7528\u7684\u306a\u4f7f\u3044\u65b9\u307e\u3067\u3001\u5e45\u5e83\u304f\u89e3\u8aac\u3057\u3066\u304d\u307e\u3057\u305f\u3002\u3053\u3053\u3067\u306f\u3001\u3053\u308c\u307e\u3067\u306e\u5185\u5bb9\u3092\u632f\u308a\u8fd4\u308a\u3064\u3064\u3001Cython\u30de\u30b9\u30bf\u30fc\u3078\u306e\u7b2c\u4e00\u6b69\u3092\u8e0f\u307f\u51fa\u3059\u305f\u3081\u306e\u30a2\u30c9\u30d0\u30a4\u30b9\u3092\u307e\u3068\u3081\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-25\">Cython\u306e\u30e1\u30ea\u30c3\u30c8\u3068\u6ce8\u610f\u70b9\u306e\u518d\u78ba\u8a8d<\/h3>\n\n\n\n<p>Cython\u306e\u30e1\u30ea\u30c3\u30c8\u306f\u3001\u4f55\u3068\u3044\u3063\u3066\u3082Python\u306e\u5b9f\u884c\u901f\u5ea6\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3067\u304d\u308b\u3053\u3068\u3067\u3059\u3002\u307e\u305f\u3001C\u306e\u95a2\u6570\u3084\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u76f4\u63a5\u5229\u7528\u3067\u304d\u308b\u305f\u3081\u3001Python\u3068\u30b7\u30fc\u30e0\u30ec\u30b9\u306b\u9023\u643a\u3067\u304d\u308b\u306e\u3082\u5927\u304d\u306a\u5229\u70b9\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u305f\u3060\u3057\u3001Cython\u3092\u4f7f\u3044\u3053\u306a\u3059\u305f\u3081\u306b\u306f\u3001C\u306e\u77e5\u8b58\u304c\u5fc5\u8981\u4e0d\u53ef\u6b20\u3067\u3059\u3002\u307e\u305f\u3001\u30c7\u30d0\u30c3\u30b0\u304c\u96e3\u3057\u304f\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3053\u3068\u3001\u30b3\u30f3\u30d1\u30a4\u30eb\u6642\u9593\u304c\u304b\u304b\u308b\u3053\u3068\u306a\u3069\u3082\u6ce8\u610f\u70b9\u3068\u3057\u3066\u6319\u3052\u3089\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u306f\u3001Cython\u306e\u30e1\u30ea\u30c3\u30c8\u304c\u3088\u304f\u308f\u304b\u308b\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9Comparison\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Python\u7248\ndef fibonacci(n):\n    if n &lt; 2:\n        return n\n    return fibonacci(n - 1) + fibonacci(n - 2)<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Cython\u7248\ndef fibonacci(int n):\n    if n &lt; 2:\n        return n\n    return fibonacci(n - 1) + fibonacci(n - 2)<\/pre>\n\n\n\n<p>Cython\u7248\u3067\u306f\u3001\u578b\u5ba3\u8a00\u3092\u884c\u3046\u3060\u3051\u3067\u3001\u5287\u7684\u306b\u5b9f\u884c\u901f\u5ea6\u304c\u5411\u4e0a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-26\">Cython\u30b9\u30ad\u30eb\u30a2\u30c3\u30d7\u306e\u305f\u3081\u306e\u30ea\u30bd\u30fc\u30b9\u96c6<\/h3>\n\n\n\n<p>Cython\u306e\u30b9\u30ad\u30eb\u30a2\u30c3\u30d7\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30ea\u30bd\u30fc\u30b9\u304c\u5f79\u7acb\u3061\u307e\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\uff1a<a href=\"https:\/\/cython.readthedocs.io\/\">https:\/\/cython.readthedocs.io\/<\/a><\/li>\n\n\n\n<li>\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\uff1a<a href=\"https:\/\/cython.readthedocs.io\/en\/latest\/src\/tutorial\/index.html\">https:\/\/cython.readthedocs.io\/en\/latest\/src\/tutorial\/index.html<\/a><\/li>\n\n\n\n<li>\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u96c6\uff1a<a href=\"https:\/\/github.com\/cython\/cython\/tree\/master\/Demos\">https:\/\/github.com\/cython\/cython\/tree\/master\/Demos<\/a><\/li>\n\n\n\n<li>\u66f8\u7c4d\uff1a\u300cCython \u2013 A Guide for Python Programmers\u300d\uff08Kurt W. Smith\u8457\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u3053\u308c\u3089\u306e\u30ea\u30bd\u30fc\u30b9\u3092\u6d3b\u7528\u3057\u3066\u3001Cython\u306e\u77e5\u8b58\u3092\u6df1\u3081\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-27\">Python\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6539\u5584\u306bCython\u3092\u6d3b\u7528\u3057\u3088\u3046\uff01<\/h3>\n\n\n\n<p>Cython\u3092\u4f7f\u3063\u3066Python\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u6539\u5584\u3059\u308b\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30a2\u30c9\u30d0\u30a4\u30b9\u3092\u53c2\u8003\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u307e\u305a\u306f\u30dc\u30c8\u30eb\u30cd\u30c3\u30af\u3092\u7279\u5b9a\u3059\u308b\uff1a\u30d7\u30ed\u30d5\u30a1\u30a4\u30ea\u30f3\u30b0\u30c4\u30fc\u30eb\u3092\u4f7f\u3063\u3066\u3001\u9ad8\u901f\u5316\u3059\u3079\u304d\u90e8\u5206\u3092\u898b\u3064\u3051\u308b<\/li>\n\n\n\n<li>\u6bb5\u968e\u7684\u306b\u9ad8\u901f\u5316\u3092\u9032\u3081\u308b\uff1a\u6700\u521d\u306fPython\u3068Cython\u3092\u6df7\u5728\u3055\u305b\u3001\u5f90\u3005\u306bCython\u306e\u6bd4\u7387\u3092\u9ad8\u3081\u3066\u3044\u304f<\/li>\n\n\n\n<li>\u578b\u4ed8\u3051\u3092\u7a4d\u6975\u7684\u306b\u884c\u3046\uff1aCython\u306e\u578b\u4ed8\u3051\u3092\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u3088\u308a\u9ad8\u901f\u306a\u30b3\u30fc\u30c9\u3092\u751f\u6210\u3067\u304d\u308b<\/li>\n\n\n\n<li>\u4e26\u5217\u51e6\u7406\u3092\u691c\u8a0e\u3059\u308b\uff1aCython\u306e\u4e26\u5217\u51e6\u7406\u6a5f\u80fd\u3092\u4f7f\u3063\u3066\u3001\u30de\u30eb\u30c1\u30b3\u30a2CPU\u306e\u6027\u80fd\u3092\u5f15\u304d\u51fa\u3059<\/li>\n\n\n\n<li>\u30b3\u30fc\u30c9\u306e\u53ef\u8aad\u6027\u3092\u91cd\u8996\u3059\u308b\uff1aCython\u3092\u4f7f\u3063\u3066\u3082\u3001\u53ef\u8aad\u6027\u306e\u9ad8\u3044\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3088\u3046\u306b\u5fc3\u304c\u3051\u308b<\/li>\n<\/ol>\n\n\n\n<p>\u4ee5\u4e0b\u306f\u3001\u30d7\u30ed\u30d5\u30a1\u30a4\u30ea\u30f3\u30b0\u30c4\u30fc\u30eb\u3092\u4f7f\u3063\u3066\u30dc\u30c8\u30eb\u30cd\u30c3\u30af\u3092\u7279\u5b9a\u3059\u308b\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import cProfile\nimport pstats\n\ndef profile(func):\n    def wrapper(*args, **kwargs):\n        pr = cProfile.Profile()\n        pr.enable()\n        result = func(*args, **kwargs)\n        pr.disable()\n        ps = pstats.Stats(pr)\n        ps.sort_stats('cumulative').print_stats(10)\n        return result\n    return wrapper\n\n@profile\ndef main():\n    # \u9ad8\u901f\u5316\u3057\u305f\u3044\u51e6\u7406\n    pass\n\nif __name__ == '__main__':\n    main()<\/pre>\n\n\n\n<p>\u3053\u306e\u3088\u3046\u306b\u3001<code>cProfile<\/code>\u3068<code>pstats<\/code>\u3092\u4f7f\u3063\u3066\u3001\u95a2\u6570\u306e\u5b9f\u884c\u6642\u9593\u3092\u6e2c\u5b9a\u3057\u3001\u30dc\u30c8\u30eb\u30cd\u30c3\u30af\u3092\u7279\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>Cython\u306f\u3001Python\u306e\u9ad8\u901f\u5316\u306b\u6b20\u304b\u305b\u306a\u3044\u30c4\u30fc\u30eb\u3067\u3059\u3002\u672c\u8a18\u4e8b\u3067\u7d39\u4ecb\u3057\u305f\u5185\u5bb9\u3092\u53c2\u8003\u306b\u3001\u305c\u3072Cython\u3092\u6d3b\u7528\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002Python\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u6539\u5584\u3057\u3001\u3088\u308a\u9ad8\u5ea6\u306a\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u958b\u767a\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u3053\u3068\u3092\u9858\u3063\u3066\u3044\u307e\u3059\u3002Cython\u30de\u30b9\u30bf\u30fc\u3078\u306e\u7b2c\u4e00\u6b69\u3092\u8e0f\u307f\u51fa\u3057\u307e\u3057\u3087\u3046\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u98db\u8e8d\u7684\u306b\u5411\u4e0a\u3055\u305b\u308bCython\u3002\u305d\u306e\u57fa\u790e\u304b\u3089\u5fdc\u7528\u307e\u3067\u3001\u5b9f\u8df5\u7684\u306a\u30c6\u30af\u30cb\u30c3\u30af\u3092\u4ea4\u3048\u3066\u5fb9\u5e95\u89e3\u8aac\u3057\u307e\u3059\u3002\u6a5f\u68b0\u5b66\u7fd2\u3084\u79d1\u5b66\u8a08\u7b97\u306e\u5206\u91ce\u3067\u6ce8\u76ee\u3055\u308c\u308bCython\u306e\u9b45\u529b\u3092\u3001\u8c4a\u5bcc\u306a\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3068\u3068\u3082\u306b\u7d10\u89e3\u3044\u3066 &#8230; <\/p>\n","protected":false},"author":1,"featured_media":720,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":{"0":"post-719","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"_links":{"self":[{"href":"https:\/\/chocottopro.com\/index.php?rest_route=\/wp\/v2\/posts\/719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chocottopro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chocottopro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chocottopro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chocottopro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=719"}],"version-history":[{"count":1,"href":"https:\/\/chocottopro.com\/index.php?rest_route=\/wp\/v2\/posts\/719\/revisions"}],"predecessor-version":[{"id":721,"href":"https:\/\/chocottopro.com\/index.php?rest_route=\/wp\/v2\/posts\/719\/revisions\/721"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/chocottopro.com\/index.php?rest_route=\/wp\/v2\/media\/720"}],"wp:attachment":[{"href":"https:\/\/chocottopro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chocottopro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chocottopro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}