{"id":157537,"date":"2012-11-06T17:07:04","date_gmt":"2012-11-06T13:07:04","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=157537"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=157537","title":{"rendered":"<span class=\"post_title\">\u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432 Python. \u041b\u0435\u0433\u043a\u0438\u0439 \u043f\u0443\u0442\u044c<\/span>"},"content":{"rendered":"<div class=\"content html_format\">       \u0412\u0441\u0435\u0433\u0434\u0430 \u0437\u043d\u0430\u043b, \u0447\u0442\u043e \u043e\u0434\u043d\u043e \u0438\u0437 \u0434\u043e\u0441\u0442\u043e\u0438\u043d\u0441\u0442\u0432 Python \u2014 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0430\u043c\u044b\u0435 \u0442\u043e\u0440\u043c\u043e\u0437\u043d\u044b\u0435 \u043a\u0443\u0441\u043a\u0438 \u043a\u043e\u0434\u0430 \u043d\u0430 \u0421\u0438 \u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0434\u043e \u043d\u0435\u0434\u043e\u0441\u0442\u0438\u0436\u0438\u043c\u044b\u0445 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u0443\u0435\u043c\u044b\u043c \u044f\u0437\u044b\u043a\u0430\u043c \u0432\u044b\u0441\u043e\u0442. \u041d\u043e \u0441\u0430\u043c \u043d\u0438 \u0440\u0430\u0437\u0443 \u043d\u0435 \u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b, \u0442.\u043a. \u0441\u0447\u0438\u0442\u0430\u043b \u0447\u0442\u043e \u044d\u0442\u043e \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0441\u043b\u043e\u0436\u043d\u043e. \u041f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u0442\u0430\u043a \u043d\u0435 \u0441\u0447\u0438\u0442\u0430\u044e.<\/p>\n<p>  \u041f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u044b \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0435 \u0441 ctypes \u0432\u0440\u044f\u0434\u043b\u0438 \u043d\u0430\u0439\u0434\u0443\u0442 \u0442\u0443\u0442 \u0447\u0442\u043e-\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435, \u043d\u043e\u0432\u0438\u0447\u043a\u043e\u0432 \u0436\u0435 \u043f\u0440\u043e\u0448\u0443 \u043f\u043e\u0434 \u043a\u0430\u0442.<br \/>  <a name=\"habracut\"><\/a><br \/>  <a href=\"http:\/\/docs.python.org\/2\/library\/ctypes.html\"><b>Ctypes<\/b><\/a> \u2014 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c Python \u0434\u043b\u044f \u0438\u043c\u043f\u043e\u0440\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438\u0437 \u0432\u043d\u0435\u0448\u043d\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a.<br \/>  <b>%timeit<\/b> \u2014 magic-\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u043e\u043b\u043e\u0447\u043a\u0438 <a href=\"http:\/\/ipython.org\/\">IPython<\/a>, \u0438\u0437\u043c\u0435\u0440\u044f\u044e\u0449\u0430\u044f \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043d\u0430 Python  <\/p>\n<hr\/>\n<p>  Ctypes \u2014 \u044d\u0442\u043e \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u043e! \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u0431\u0430\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430: \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0447\u0438\u0441\u0435\u043b \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435. <br \/>  \u0412\u043e\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0430 Python  <\/p>\n<pre><code class=\"python\">def sumrange(arg):     return sum(xrange(arg)) <\/code><\/pre>\n<p>  \u041e\u0442\u043b\u0438\u0447\u043d\u043e! \u041d\u043e \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0447\u0438\u0441\u0435\u043b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043e\u0442 0 \u0434\u043e 10**8 (\u0442.\u0435. 100,000,000)  <\/p>\n<pre><code class=\"python\">In [2]: %timeit sumrange(10**2) 1000000 loops, best of 3: 1.53 us per loop  In [3]: %timeit sumrange(10**8) 1 loops, best of 3: 9.77 s per loop  In [4]: %timeit sumrange(10**9) 1 loops, best of 3: 97.8 s per loop <\/code><\/pre>\n<p>  \u0423\u0436\u0435 \u043d\u0435 \u0442\u0430\u043a \u0432\u0435\u0441\u0435\u043b\u043e. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043a\u043e\u0435-\u0447\u0442\u043e \u0434\u0440\u0443\u0433\u043e\u0435:  <\/p>\n<pre><code class=\"python\">def sumrange2(arg):     x = i = 0     while i &lt; arg:         x += i         i += 1     return x <\/code><\/pre>\n<p>  \u0427\u0442\u043e \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f?  <\/p>\n<pre><code class=\"python\">In [10]: %timeit sumrange2(10**2) 100000 loops, best of 3: 10.5 us per loop  In [11]: %timeit sumrange2(10**8) 1 loops, best of 3: 18.5 s per loop <\/code><\/pre>\n<p>  \u0412\u043e\u0442 \u044d\u0442\u043e \u0434\u0430\u2026 \u0422\u0430\u043a \u0435\u0449\u0435 \u0445\u0443\u0436\u0435\u2026 \u0412 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0434\u0430\u0436\u0435 \u043d\u0435 \u0431\u0443\u0434\u0443 \u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c 10**9.<br \/>  \u0422\u0430\u043a \u043a\u0430\u043a \u0436\u0435 \u043d\u0430\u043c \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435? \u0422\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0439\u0442\u0435 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438\u2026 \u043c\u044b \u0436\u0435 \u0432 \u043d\u043e\u0432\u043e\u043c \u043c\u0438\u0440\u0435 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043e\u0432! (<i>\u0432 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u0435: don&#8217;t suggest math tricks\u2026 this is the the new world of computing!<\/i>) <br \/>  \u0414\u0430, \u044f \u0437\u043d\u0430\u044e, \u0447\u0442\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u2014 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0430 \u0438 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u044b \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430, n*(n+1)\/2. \u041d\u043e \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u0430 \u043d\u0435 \u044d\u0442\u043e\u043c\u0443. <\/p>\n<p>  \u041a\u0430\u043a \u043d\u0430\u0441\u0447\u0435\u0442 ctypes?<\/p>\n<pre><code>#include &lt;stdio.h&gt;  unsigned long long sumrange(unsigned long long arg) {     unsigned long long i, x;     x = 0;      for (i = 0; i &lt; arg; i++) {         x = x + i;     }     return x; }<\/code><\/pre>\n<p>  \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u043c \u0441 \u0438\u043c\u0435\u043d\u0435\u043c sumrange.c \u0438 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c (\u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u0447\u0438\u0441\u0442\u043e\u0442\u044b \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0430):<br \/>  <code>$ gcc -shared -Wl,-install_name,sumrange.so -o sumrange.so -fPIC sumrange.c<\/code><\/p>\n<p>  \u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0432 Python \u0442\u043e \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c:  <\/p>\n<pre><code class=\"python\">import ctypes sumrange_ctypes = ctypes.CDLL('.\/sumrange.so').sumrange sumrange_ctypes.restype = ctypes.c_ulonglong sumrange_ctypes.argtypes = ctypes.c_ulonglong, <\/code><\/pre>\n<p>  \u0418 \u041e\u0441\u043a\u0430\u0440 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u2026 <\/p>\n<pre><code class=\"python\">In [15]: %timeit sumrange_ctypes(10**2) 1000000 loops, best of 3: 1.28 us per loop  In [16]: %timeit sumrange_ctypes(10**8) 1 loops, best of 3: 381 ms per loop  In [17]: %timeit sumrange_ctypes(10**9) 1 loops, best of 3: 3.79 s per loop  In [18]: %timeit sumrange_ctypes(10**10) 1 loops, best of 3: 37.8 s per loop <\/code><\/pre>\n<p>  \u0418\u0442\u043e\u0433\u043e\u0432\u0430\u044f \u0441\u0432\u043e\u0434\u043a\u0430:  <\/p>\n<table>\n<tr>\n<th><\/th>\n<th>10**2<\/th>\n<th>10**8<\/th>\n<th>10**9<\/th>\n<th>10**10<\/th>\n<\/tr>\n<tr>\n<td>\u0427\u0438\u0441\u0442\u044b\u0439 Python, \u0441\u043f\u043e\u0441\u043e\u0431 \u21161<\/td>\n<td>1.53 \u043c\u043a\u0441<\/td>\n<td>9.77 \u0441<\/td>\n<td>97.8 \u0441<\/td>\n<td>&#8212;<\/td>\n<\/tr>\n<tr>\n<td>\u0427\u0438\u0441\u0442\u044b\u0439 Python, \u0441\u043f\u043e\u0441\u043e\u0431 \u21162<\/td>\n<td>10.5 \u043c\u043a\u0441<\/td>\n<td>18.5 \u0441<\/td>\n<td>&#8212;<\/td>\n<td>&#8212;<\/td>\n<\/tr>\n<tr>\n<td>ctypes<\/td>\n<td>1.28 \u043c\u043a\u0441<\/td>\n<td>381 \u043c\u0441<\/td>\n<td>3.79 \u0441<\/td>\n<td>37.8 \u0441<\/td>\n<\/tr>\n<\/table>\n<p>  \u0410\u0434\u0441\u043a\u0438\u0439 \u043f\u0440\u0438\u0440\u043e\u0441\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438!<\/p>\n<p>  \u0414\u043b\u044f Node.js \u0445\u0430\u043a\u0435\u0440\u043e\u0432, \u0435\u0441\u0442\u044c \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442 ctypes \u2014 FFI (Foreign Function Interface): <a href=\"https:\/\/github.com\/rbranson\/node-ffi\">github.com\/rbranson\/node-ffi<\/a>          \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/157537\/\"> http:\/\/habrahabr.ru\/post\/157537\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">       \u0412\u0441\u0435\u0433\u0434\u0430 \u0437\u043d\u0430\u043b, \u0447\u0442\u043e \u043e\u0434\u043d\u043e \u0438\u0437 \u0434\u043e\u0441\u0442\u043e\u0438\u043d\u0441\u0442\u0432 Python \u2014 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0430\u043c\u044b\u0435 \u0442\u043e\u0440\u043c\u043e\u0437\u043d\u044b\u0435 \u043a\u0443\u0441\u043a\u0438 \u043a\u043e\u0434\u0430 \u043d\u0430 \u0421\u0438 \u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0434\u043e \u043d\u0435\u0434\u043e\u0441\u0442\u0438\u0436\u0438\u043c\u044b\u0445 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u0443\u0435\u043c\u044b\u043c \u044f\u0437\u044b\u043a\u0430\u043c \u0432\u044b\u0441\u043e\u0442. \u041d\u043e \u0441\u0430\u043c \u043d\u0438 \u0440\u0430\u0437\u0443 \u043d\u0435 \u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b, \u0442.\u043a. \u0441\u0447\u0438\u0442\u0430\u043b \u0447\u0442\u043e \u044d\u0442\u043e \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0441\u043b\u043e\u0436\u043d\u043e. \u041f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u0442\u0430\u043a \u043d\u0435 \u0441\u0447\u0438\u0442\u0430\u044e.<\/p>\n<p>  \u041f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u044b \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0435 \u0441 ctypes \u0432\u0440\u044f\u0434\u043b\u0438 \u043d\u0430\u0439\u0434\u0443\u0442 \u0442\u0443\u0442 \u0447\u0442\u043e-\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435, \u043d\u043e\u0432\u0438\u0447\u043a\u043e\u0432 \u0436\u0435 \u043f\u0440\u043e\u0448\u0443 \u043f\u043e\u0434 \u043a\u0430\u0442.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-157537","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/157537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=157537"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/157537\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=157537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=157537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=157537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}