{"id":198588,"date":"2013-10-22T19:40:02","date_gmt":"2013-10-22T15:40:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=198588"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=198588","title":{"rendered":"<span class=\"post_title\">\u0427\u0442\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 while (true) \u0438\u043b\u0438 for (;;)?<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u0412 \u0441\u044b\u0440\u0446\u0430\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u0432\u0438\u0434\u0435\u043b \u044f \u0440\u0430\u0437\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u0435\u0447\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430. \u0427\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u043c\u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b\u0438\u0441\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435:  <\/p>\n<pre><code class=\"cpp\">while (true) { ... } <\/code><\/pre>\n<p>  \u0438  <\/p>\n<pre><code class=\"cpp\">for (;;) { ... } <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043a\u0430\u0436\u0434\u044b\u0439 \u0437\u0430\u0449\u0438\u0449\u0430\u043b \u201c\u0441\u0432\u043e\u0439 \u0432\u0435\u0447\u043d\u044b\u0439 \u0446\u0438\u043a\u043b\u201d \u043a\u0430\u043a \u0440\u043e\u0434\u043d\u043e\u0433\u043e, \u044f \u0440\u0435\u0448\u0438\u043b \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f. \u041a\u0442\u043e \u0436\u0435 \u043f\u0438\u0448\u0435\u0442 \u0431\u043e\u043b\u0435\u0435 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u042f \u043d\u0430\u043f\u0438\u0441\u0430\u043b 2 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0430:<\/p>\n<p>  while.c:  <\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt;  int main (int argc, char* argv[]) {     while(1){        printf(&quot;1\\n&quot;);     } }  <\/code><\/pre>\n<p>  for.c:  <\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h&gt;  int main (int argc, char* argv[]) {     for(;;){         printf(&quot;1\\n&quot;);     } } <\/code><\/pre>\n<p>  \u0421\u043e\u0431\u0440\u0430\u043b \u0438\u0445:  <\/p>\n<pre><code class=\"bash\">$ gcc -O3 while.c -o while.o3 $ gcc -O2 while.c -o while.o2 $ gcc -O1 while.c -o while.o1 $ gcc -O3 for.c -o for.o3 $ gcc -O2 for.c -o for.o2 $ gcc -O1 for.c -o for.o1 <\/code><\/pre>\n<p>  \u0418 \u0434\u0435\u0437\u0430\u0441\u0441\u0435\u043c\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043b. \u041a\u043e\u043c\u0443 \u043b\u0435\u043d\u044c \u0447\u0438\u0442\u0430\u0442\u044c \u0430\u0441\u0441\u0435\u043c\u0431\u043b\u0435\u0440\u043d\u044b\u0435 \u043b\u0438\u0441\u0442\u043d\u0438\u0433\u0438 \u2014 \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u043a\u0440\u0443\u0442\u0438\u0442\u044c \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u0432\u043d\u0438\u0437. \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043b\u0438\u0441\u0442\u0438\u043d\u0433\u0438:  <\/p>\n<pre><code class=\"bash\"> $ objdump -d .\/while.o3 ... 0000000000400430 &lt;main&gt;:  400430:       48 83 ec 08             sub    $0x8,%rsp  400434:       0f 1f 40 00             nopl   0x0(%rax)  400438:       bf d4 05 40 00          mov    $0x4005d4,%edi  40043d:       e8 be ff ff ff          callq  400400 &lt;puts@plt&gt;  400442:       eb f4                   jmp    400438 &lt;main+0x8&gt; ... $ objdump -d .\/while.o2 ... 0000000000400430 &lt;main&gt;:  400430:       48 83 ec 08             sub    $0x8,%rsp  400434:       0f 1f 40 00             nopl   0x0(%rax)  400438:       bf d4 05 40 00          mov    $0x4005d4,%edi  40043d:       e8 be ff ff ff          callq  400400 &lt;puts@plt&gt;  400442:       eb f4                   jmp    400438 &lt;main+0x8&gt; ... $ objdump -d .\/while.o1 ... 000000000040051c &lt;main&gt;:  40051c:       48 83 ec 08             sub    $0x8,%rsp  400520:       bf d4 05 40 00          mov    $0x4005d4,%edi  400525:       e8 d6 fe ff ff          callq  400400 &lt;puts@plt&gt;  40052a:       eb f4                   jmp    400520 &lt;main+0x4&gt; ... $ objdump -d .\/for.o1 ... 000000000040051c &lt;main&gt;:  40051c:       48 83 ec 08             sub    $0x8,%rsp  400520:       bf d4 05 40 00          mov    $0x4005d4,%edi  400525:       e8 d6 fe ff ff          callq  400400 &lt;puts@plt&gt;  40052a:       eb f4                   jmp    400520 &lt;main+0x4&gt; ... $ objdump -d .\/for.o2 ... 0000000000400430 &lt;main&gt;:  400430:       48 83 ec 08             sub    $0x8,%rsp  400434:       0f 1f 40 00             nopl   0x0(%rax)  400438:       bf d4 05 40 00          mov    $0x4005d4,%edi  40043d:       e8 be ff ff ff          callq  400400 &lt;puts@plt&gt;  400442:       eb f4                   jmp    400438 &lt;main+0x8&gt; ... $ objdump -d .\/for.o3 0000000000400430 &lt;main&gt;:  400430:       48 83 ec 08             sub    $0x8,%rsp  400434:       0f 1f 40 00             nopl   0x0(%rax)  400438:       bf d4 05 40 00          mov    $0x4005d4,%edi  40043d:       e8 be ff ff ff          callq  400400 &lt;puts@plt&gt;  400442:       eb f4                   jmp    400438 &lt;main+0x8&gt;  <\/code><\/pre>\n<h4>\u0420\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c \u043d\u0430 \u043f\u0430\u043b\u044c\u0446\u0430\u0445<\/h4>\n<p>  \u0420\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0435 \u043f\u043e\u0432\u043b\u0438\u044f\u043b\u0438 \u043d\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0446\u0438\u043a\u043b\u0430 while (true) \u2014 \u043e\u043d \u0432\u0441\u0435\u0433\u0434\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u043b 3 \u043a\u043e\u043c\u0430\u043d\u0434\u044b: mov, callq \u0438 jmp. \u0422\u0430\u043a \u0436\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0435 \u043f\u043e\u0432\u043b\u0438\u044f\u043b\u0438 \u043d\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e for \u2014 \u043e\u043d \u0442\u043e\u0436\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0431\u044b\u043b \u0438\u0437 3\u0445 \u043a\u043e\u043c\u0430\u043d\u0434: mov, callq, jmp. \u041c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439 mov, callq \u0438 jmp \u043d\u0438\u0447\u0435\u043c \u043d\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u043b\u0438\u0441\u044c. \u0414\u043b\u0438\u043d\u043d\u0430 \u043a\u043e\u043c\u0430\u043d\u0434 \u0432 \u0431\u0430\u0439\u0442\u0430\u0445 \u0432\u043e \u0432\u0441\u0435\u0445 6\u0438 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u0430.<\/p>\n<p>  \u0415\u0441\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u043c\u0435\u0436\u0434\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f\u043c\u0438 -O1 \u0438 -O2\/-O3 jmp \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u043b\u0441\u044f \u043d\u0430 main+4 \u0430 \u043d\u0435 \u043d\u0430 main+8, \u043d\u043e \u0441 \u0443\u0447\u0435\u0442\u043e\u043c \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u044d\u0442\u043e \u0441\u0442\u0430\u0442\u0438\u0447\u043d\u044b\u0439 \u0430\u0434\u0440\u0435\u0441 (\u043a\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u0438\u0437 asm-\u043a\u043e\u0434\u0430) \u043e\u043d\u043e \u0442\u043e\u0436\u0435 \u043d\u0435 \u043d\u0435\u0441\u0435\u0442 \u0440\u0430\u0437\u043d\u0438\u0446\u044b \u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438\u2026 \u0425\u043e\u0442\u044f\u2026 \u0430 \u0432\u0434\u0440\u0443\u0433 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u043f\u0430\u043c\u044f\u0442\u0438 \u0440\u0430\u0437\u043d\u044b\u0435, \u0432\u0435\u0434\u044c \u043d\u0430 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044f \u0437\u043d\u0430\u044e \u0434\u043b\u044f \u0442\u0435\u043b\u043e\u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0439 \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430\u043c\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 x86 (\u0438 amd64) \u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0443\u0441\u0438\u043b\u0438\u044f \u043f\u0440\u043e\u0446\u0430!<\/p>\n<p>  \u0423\u0437\u043d\u0430\u0435\u043c:<br \/>  400438\/4096 = 97,763183594<br \/>  400520\/4096 = 97,783203125<\/p>\n<p>  \u041f\u0440\u043e\u043d\u0435\u0441\u043b\u043e. \u0421\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u043f\u0430\u043c\u044f\u0442\u0438 \u043e\u0434\u043d\u0430. \u0414\u0430 \u044d\u0442\u043e 97 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0412\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u0412\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0430\u0434\u0440\u0435\u0441\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430. \u041d\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d\u0430 \u043d\u0430\u043c \u0438 \u043d\u0443\u0436\u043d\u0430.<\/p>\n<h4>\u0418\u0442\u043e\u0433<\/h4>\n<p>  while (true) \u0438 for (;;) \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u044b \u043f\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439 \u0438 \u0441 \u043b\u044e\u0431\u044b\u043c\u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f\u043c\u0438 -Ox. \u0422\u0430\u043a \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0412\u0430\u0441 \u0441\u043f\u0440\u043e\u0441\u044f\u0442 \u043a\u0442\u043e \u0438\u0437 \u043d\u0438\u0445 \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u2014 \u0441\u043c\u0435\u043b\u043e \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u0435 \u0447\u0442\u043e \u201cfor (;;)\u201d \u2014 8 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c \u201cwhile (true)\u201d \u2014 12 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432.<\/p>\n<p>  \u0414\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u043d\u0435 \u0432\u0435\u0440\u0438\u0442 \u0447\u0442\u043e \u0431\u0435\u0437 -Ox \u0431\u0443\u0434\u0435\u0442 \u0442\u043e\u0436\u0435 \u0441\u0430\u043c\u043e\u0435:  <\/p>\n<pre><code class=\"bash\">$ gcc while.c -o while.noO $ objdump -d while.noO ...  40052b:       bf e4 05 40 00          mov    $0x4005e4,%edi  400530:       e8 cb fe ff ff          callq  400400 &lt;puts@plt&gt;  400535:       eb f4                   jmp    40052b &lt;main+0xf&gt; ... $ gcc for.c -o for.noO $ objdump -d for.noO ...  40052b:       bf e4 05 40 00          mov    $0x4005e4,%edi  400530:       e8 cb fe ff ff          callq  400400 &lt;puts@plt&gt;  400535:       eb f4                   jmp    40052b &lt;main+0xf&gt; ... <\/code><\/pre>\n<p>  <b>P.S.<\/b> \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0432\u0441\u0435 \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0430\u0432\u0434\u043e\u0439 \u043d\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0435 \u201cgcc version 4.7.2 (Debian 4.7.2-5)\u201d    \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\/198588\/\"> http:\/\/habrahabr.ru\/post\/198588\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u0412 \u0441\u044b\u0440\u0446\u0430\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u0432\u0438\u0434\u0435\u043b \u044f \u0440\u0430\u0437\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u0435\u0447\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430. \u0427\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u043c\u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b\u0438\u0441\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435:  <\/p>\n<pre><code class=\"cpp\">while (true) { ... } <\/code><\/pre>\n<p>  \u0438  <\/p>\n<pre><code class=\"cpp\">for (;;) { ... } <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043a\u0430\u0436\u0434\u044b\u0439 \u0437\u0430\u0449\u0438\u0449\u0430\u043b \u201c\u0441\u0432\u043e\u0439 \u0432\u0435\u0447\u043d\u044b\u0439 \u0446\u0438\u043a\u043b\u201d \u043a\u0430\u043a \u0440\u043e\u0434\u043d\u043e\u0433\u043e, \u044f \u0440\u0435\u0448\u0438\u043b \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f. \u041a\u0442\u043e \u0436\u0435 \u043f\u0438\u0448\u0435\u0442 \u0431\u043e\u043b\u0435\u0435 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434.  <\/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-198588","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/198588","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=198588"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/198588\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=198588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=198588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=198588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}