{"id":343108,"date":"2022-12-25T15:00:11","date_gmt":"2022-12-25T15:00:11","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=343108"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2022-12-25T15:00:11","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=343108","title":{"rendered":"<span>\u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0438\u0433\u0440\u044b \u00ab\u0416\u0438\u0437\u043d\u044c\u00bb \u043d\u0430 Julia<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u042d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e\u0439 case \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438. \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043a\u0440\u0430\u0439\u043d\u0435 \u043f\u0440\u043e\u0441\u0442 \u0438 \u0435\u0433\u043e \u0437\u043d\u0430\u044e\u0442 \u0432\u0441\u0435. \u041d\u043e \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c!<\/p>\n<h2>1. Julia, \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u043f\u0435\u0440\u0432\u0430\u044f \u0438 \u043d\u0430\u0438\u0432\u043d\u0430\u044f<\/h2>\n<p>Julia \u0445\u043e\u0440\u043e\u0448\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442 \u0438\u0437 \u0441\u0435\u0431\u044f \u043f\u0438\u0442\u043e\u043d\u043e-\u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u044f\u0437\u044b\u043a \u0441 \u0443\u0442\u0438\u043d\u043e\u0439 \u0442\u0438\u043f\u0438\u0437\u0430\u0446\u0438\u0435\u0439, \u0431\u0443\u0434\u0443\u0447\u0438 \u043f\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u043d\u0435 \u043f\u0438\u0442\u043e\u043d\u043e \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0439. \u041f\u0440\u0438 \u0442\u043e\u043c \u0447\u0442\u043e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0443 \u043d\u0435\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u041f\u0438\u0442\u043e\u043d\u0430 (\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0442\u0441\u044f \u0441 1), \u043f\u043e\u0447\u0442\u0438 \u043a\u0430\u0436\u0434\u0443\u044e \u0441\u0442\u0440\u043e\u0447\u043a\u0443 \u043d\u0430\u0434\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c, \u043d\u043e \u0432\u0441\u0435 \u044d\u0442\u0438 \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u043d\u0438\u044f \u043a\u0430\u0436\u0443\u0442\u0441\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u044b\u043c\u0438, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0435\u0435 \u0445\u043e\u0440\u043e\u0448\u0438\u043c \u044f\u0437\u044b\u043a\u043e\u043c \u0434\u043b\u044f \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u043a\u043e\u0433\u0434\u0430 Python \u0443\u0436\u0435 \u0436\u043c\u0435\u0442. \u041d\u043e \u044d\u0442\u0430 \u043f\u043e\u0445\u043e\u0436\u0435\u0441\u0442\u044c \u043c\u043e\u0436\u0435\u0442 \u0441\u044b\u0433\u0440\u0430\u0442\u044c \u0434\u0443\u0440\u043d\u0443\u044e \u0441\u043b\u0443\u0436\u0431\u0443.<\/p>\n<p>\u041d\u0430\u0448\u0430 \u043f\u0435\u0440\u0432\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f:<\/p>\n<pre><code class=\"julia\">global size = 10000 global board=zeros(Int8, size, size)  global helper=zeros(Int8, size, size)  function step0()   global size, board, helper   for x in 1:size     left = ((x == 1) ? size : x-1)     lower = size     right = ((x == size) ? 1 : x+1)     for y in 1:size       upper = ((y == size) ? 1 : y+1)       # get the number of neighbours       nb = board[left, upper] + board[x, upper] + board[right, upper] +                  board[left, y] +                            board[right, y] +                  board[left, lower] + board[x, lower]      + board[right, lower]       cell = board[x, y]       w = 0       if (nb == 3) &amp;&amp; (cell == 0)          w = 1 # new born       elseif (nb &lt; 2) &amp;&amp; (cell == 1)          w = 1 # die lonely       elseif (nb > 3) &amp;&amp; (cell == 1)          w = 1 # die from owercrowding       end       helper[x, y] = w       lower = y     end   end      # now move modifications back   Threads.@threads for x in 1:size     for y in 1:size   if helper[x,y] > 0       board[x,y] = 1 - board[x,y]   end     end   end      return end <\/code><\/pre>\n<p>\u0414\u0430\u0435\u0442 \u0432\u0440\u0435\u043c\u044f <strong>85.486411 seconds (1.02 G allocations: 18.188 GiB, 1.83% gc time)<\/strong>. \u0427\u0442\u043e? \u041b\u0438\u0448\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 Python?<\/p>\n<p>\u041c\u044b \u0441\u043e\u0432\u0435\u0440\u0448\u0438\u043b\u0438 \u0441\u0442\u0440\u0430\u0448\u043d\u0443\u044e \u043e\u0448\u0438\u0431\u043a\u0443. \u041f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u043e\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e allocations. \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0432 Julia \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0438\u043c\u0435\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u0442\u0438\u043f. \u041f\u043e\u044d\u0442\u043e\u043c\u0443, \u043a\u043e\u0433\u0434\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435, \u0442\u043e \u043e\u043d\u0430 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0443\u0432\u0435\u0440\u0435\u043d\u0430 \u0432 \u0438\u0445 \u0442\u0438\u043f\u0435, \u0438 \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d\u0430 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 run time. \u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u043c \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0447\u0435\u0440\u0435\u0437 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:<\/p>\n<h2>2. \u041f\u0435\u0440\u0432\u0430\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<pre><code class=\"julia\">function step1(size, board, helper)   for x in 1:size     left = ((x == 1) ? size : x-1)     lower = size     right = ((x == size) ? 1 : x+1)     for y in 1:size       upper = ((y == size) ? 1 : y+1)       # get the number of neighbours       nb = board[left, upper] + board[x, upper] + board[right, upper] +                  board[left, y] +                            board[right, y] +                  board[left, lower] + board[x, lower]      + board[right, lower]       cell = board[x, y]       w = 0       if (nb == 3) &amp;&amp; (cell == 0)          w = 1 # new born       elseif (nb &lt; 2) &amp;&amp; (cell == 1)          w = 1 # die lonely       elseif (nb > 3) &amp;&amp; (cell == 1)          w = 1 # die from owercrowding       end       helper[x, y] = w       lower = y     end   end      # now move modifications back   for x in 1:size     for y in 1:size   if helper[x,y] > 0       board[x,y] = 1 - board[x,y]   end     end   end      return end<\/code><\/pre>\n<p><strong>1.882592 seconds<\/strong><\/p>\n<p>\u041d\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0439. \u0412\u043e\u0442 \u044d\u0442\u043e \u0443\u0436\u0435 \u0434\u0440\u0443\u0433\u043e\u0435 \u0434\u0435\u043b\u043e. \u041c\u044b \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 <strong>53M<\/strong> \u043a\u043b\u0435\u0442\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443 (\u043d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u044e, \u044d\u0442\u043e <strong>AMD Ryzen 5 3600X 6-Core Processor 3.79 GHz<\/strong>) <\/p>\n<h2>3. Multi-threading<\/h2>\n<p>\u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, \u0447\u0430\u0441\u0442\u043e \u0440\u0430\u0441\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u043d\u0430 Julia \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u043e &#8212; \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043c Threads<a class=\"mention\" href=\"\/users\/threads\">@threads<\/a>\u043f\u0435\u0440\u0435\u0434 \u0446\u0438\u043a\u043b\u0430\u043c\u0438. <\/p>\n<pre><code class=\"julia\">function step2(size, board, helper)   Threads.@threads for x in 1:size     left = ((x == 1) ? size : x-1)     lower = size     right = ((x == size) ? 1 : x+1)     for y in 1:size       upper = ((y == size) ? 1 : y+1)       # get the number of neighbours       nb = board[left, upper] + board[x, upper] + board[right, upper] +                  board[left, y] +                            board[right, y] +                  board[left, lower] + board[x, lower]      + board[right, lower]       cell = board[x, y]       w = 0       if (nb == 3) &amp;&amp; (cell == 0)          w = 1 # new born       elseif (nb &lt; 2) &amp;&amp; (cell == 1)          w = 1 # die lonely       elseif (nb > 3) &amp;&amp; (cell == 1)          w = 1 # die from owercrowding       end       helper[x, y] = w       lower = y     end   end      # now move modifications back   Threads.@threads for x in 1:size     for y in 1:size   if helper[x,y] > 0       board[x,y] = 1 - board[x,y]   end     end   end      return end <\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0442\u0435\u043c, \u0447\u0442\u043e \u0446\u0438\u043a\u043b\u044b \u043d\u0435 \u043f\u0435\u0440\u0435\u0441\u0435\u043a\u0430\u044e\u0442\u0441\u044f \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u043e \u0437\u0430\u043f\u0438\u0441\u0438. \u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0431\u0443\u0434\u0435\u043c \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u0442\u0440\u0435\u0434\u0435 \u0438 \u0441 <strong>&#8212;threads 12<\/strong>:<\/p>\n<p>1 thread: <strong>1.981022 seconds (12 allocations: 1.375 KiB)<\/strong><\/p>\n<p>12 threads: <strong>0.447547 seconds (124 allocations: 13.438 KiB)<\/strong><\/p>\n<p>\u041c\u044b \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 <strong>223M<\/strong> \u043a\u043b\u0435\u0442\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443<\/p>\n<h2>4. inbounds, \u043e\u0442\u043c\u0435\u043d\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430<\/h2>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u043c\u044b \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u0443\u0432\u0435\u0440\u0435\u043d\u044b \u0432 \u0441\u0435\u0431\u0435, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u044b\u043a\u043b\u044e\u0447\u0438\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438:<\/p>\n<pre><code class=\"julia\">function step3(size, board, helper)   Threads.@threads for x in 1:size     left = ((x == 1) ? size : x-1)     lower = size     right = ((x == size) ? 1 : x+1)     for y in 1:size       upper = ((y == size) ? 1 : y+1)       # get the number of neighbours       @inbounds nb = board[left, upper] + board[x, upper] + board[right, upper] +                  board[left, y] +                            board[right, y] +                  board[left, lower] + board[x, lower]      + board[right, lower]       @inbounds cell = board[x, y]       w = 0       if (nb == 3) &amp;&amp; (cell == 0)          w = 1 # new born       elseif (nb &lt; 2) &amp;&amp; (cell == 1)          w = 1 # die lonely       elseif (nb > 3) &amp;&amp; (cell == 1)          w = 1 # die from owercrowding       end       @inbounds helper[x, y] = w       lower = y     end   end      # now move modifications back   Threads.@threads for x in 1:size     for y in 1:size   @inbounds if helper[x,y] > 0       @inbounds board[x,y] = 1 - board[x,y]   end     end   end      return end<\/code><\/pre>\n<p>1 thread: <strong>1.584128 seconds (12 allocations: 1.375 KiB)<\/strong><\/p>\n<p>12 threads: <strong>0.348541 seconds (127 allocations: 13.531 KiB)<\/strong><\/p>\n<p>\u041c\u044b \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 <strong>286M<\/strong> \u043a\u043b\u0435\u0442\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443<\/p>\n<h2>5. Cache-friendly code<\/h2>\n<p>\u0410 \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u043f\u043e\u043c\u0435\u043d\u044f\u0435\u043c \u0432\u0435\u0437\u0434\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b x,y \u043d\u0430 y,x?<\/p>\n<pre><code class=\"julia\">function step4(size, board, helper)   Threads.@threads for x in 1:size     left = ((x == 1) ? size : x-1)     lower = size     right = ((x == size) ? 1 : x+1)     for y in 1:size       upper = ((y == size) ? 1 : y+1)       # get the number of neighbours       @inbounds nb = board[upper, left] + board[upper, x] + board[upper, right] +                  board[y, left] +                            board[y, right] +                  board[lower, left] + board[lower, x]      + board[lower, right]       @inbounds cell = board[y, x]       w = 0       if (nb == 3) &amp;&amp; (cell == 0)          w = 1 # new born       elseif (nb &lt; 2) &amp;&amp; (cell == 1)          w = 1 # die lonely       elseif (nb > 3) &amp;&amp; (cell == 1)          w = 1 # die from owercrowding       end       @inbounds helper[y, x] = w       lower = y     end   end      # now move modifications back   Threads.@threads for x in 1:size     for y in 1:size   @inbounds if helper[y,x] > 0       @inbounds board[y,x] = 1 - board[y,x]   end     end   end      return end<\/code><\/pre>\n<p>1 threads: <strong>0.842495 seconds (12 allocations: 1.375 KiB)<\/strong><\/p>\n<p>12 threads: <strong>0.113791 seconds (125 allocations: 13.469 KiB)<\/strong><\/p>\n<p>\u041c\u044b \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 <strong>879M<\/strong> \u043a\u043b\u0435\u0442\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443<\/p>\n<p>\u041d\u043e \u043f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e? \u0414\u0435\u043b\u043e \u0432 \u0443\u0440\u043e\u0432\u043d\u044f\u0445 \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438. \u041f\u0440\u0438 \u043e\u0431\u0445\u043e\u0434\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u043e\u0434\u0438\u043d \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430 \u043c\u043e\u043d\u043e\u0442\u043e\u043d\u043d\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442 \u0430\u0434\u0440\u0435\u0441 \u0434\u043e\u0441\u0442\u0443\u043f\u0430, \u0442\u043e\u0433\u0434\u0430 \u043a\u0430\u043a \u0434\u0440\u0443\u0433\u043e\u0439 \u0447\u0438\u0442\u0430\u0435\u0442 \u0431\u0430\u0439\u0442\u044b &#171;\u0432 \u0440\u0430\u0437\u0431\u0438\u0432\u043a\u0443&#187;. \u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e, \u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043c\u0430\u0442\u0440\u0438\u0446\u0430\u0445 \u044d\u0442\u043e \u0447\u043e\u0435\u043d\u044c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e.<\/p>\n<h2>6. \u0423\u0431\u0438\u0440\u0430\u0435\u043c \u043f\u0435\u0440\u0432\u044b\u0439 if<\/h2>\n<p>\u041e\u0442\u043c\u0435\u043d\u0430 \u043f\u0440\u0432\u0435\u0440\u043e\u043a \u0433\u0440\u0430\u043d\u0438\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u043d\u0430\u043c \u043f\u043e\u043c\u043e\u0433\u043b\u0430 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u0447\u0430\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0432\u044b\u0431\u0440\u043e\u0449\u0435\u043d\u043d\u043e\u0439, \u043d\u043e \u0438 \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043c\u044b \u0438\u0437\u0431\u0430\u0432\u0438\u043b\u0438\u0441\u044c \u043e\u0442 if, \u0430 \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0432\u0430\u0442\u044b \u0441\u0440\u044b\u0432\u043e\u043c \u043a\u043e\u043d\u0432\u0435\u0439\u0435\u0440\u0430. \u0415\u0441\u043b\u0438 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0433\u0440\u0430\u043d\u0438\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u043d\u0430 \u0438\u0437 \u0432\u0435\u0442\u043e\u043a \u043f\u043e\u0447\u0442\u0438 \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f, \u0442\u043e \u0432\u043e\u0442 \u044d\u0442\u043e\u0442 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"julia\">  if helper[y,x] > 0       board[y,x] = 1 - board[y,x]   end<\/code><\/pre>\n<p>\u0443\u0436\u0430\u0441\u0435\u043d. \u0443 \u044d\u0442\u043e\u0433\u043e IF \u043d\u0435\u0442 &#8216;\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0439&#8217; \u0441\u0442\u043e\u0440\u043e\u043d\u044b. \u043d\u043e \u043a\u0430\u043a \u0432\u043e\u043e\u0431\u0449\u0435 \u043c\u043d\u0435 \u043f\u0440\u0438\u0448\u043b\u0430 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u0438\u0434\u0435\u044f \u0432 helper \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u0437\u043d\u0430\u043a, \u0447\u0442\u043e \u043a\u043b\u0435\u0442\u043a\u0430 \u043f\u043e\u043c\u0435\u043d\u044f\u043b\u0430\u0441\u044c? \u041f\u0440\u043e\u0441\u0442\u043e \u044f DBA \u0438 \u043f\u043e\u0434\u0441\u043e\u0437\u043d\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b \u043e\u0431\u044a\u0435\u043c UPDATE. \u0410 \u044d\u0442\u043e\u0433\u043e \u0437\u0434\u0435\u0441\u044c \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u043d\u0430\u0434\u043e. \u0412 helper \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435:<\/p>\n<pre><code class=\"julia\">function step5(size, board, helper)   Threads.@threads for x in 1:size     left = ((x == 1) ? size : x-1)     lower = size     right = ((x == size) ? 1 : x+1)     for y in 1:size       upper = ((y == size) ? 1 : y+1)       # get the number of neighbours       @inbounds nb = board[upper, left] + board[upper, x] + board[upper, right] +                  board[y, left] +                            board[y, right] +                  board[lower, left] + board[lower, x]      + board[lower, right]       @inbounds cell = board[y, x]       w = cell       if (nb == 3) &amp;&amp; (cell == 0)          w = 1 # new born       elseif (nb &lt; 2) &amp;&amp; (cell == 1)          w = 0 # die lonely       elseif (nb > 3) &amp;&amp; (cell == 1)          w = 0 # die from owercrowding       end       @inbounds helper[y, x] = w       lower = y     end   end      # now move modifications back   Threads.@threads for x in 1:size     for y in 1:size       @inbounds board[y,x] = helper[y,x]     end   end      return end<\/code><\/pre>\n<p>1 threads: <strong>0.725642 seconds (12 allocations: 1.375 KiB)<\/strong><\/p>\n<p>12 threads: <strong>0.089742 seconds (148 allocations: 16.094 KiB)<\/strong><\/p>\n<p>\u041c\u044b \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 <strong>1.114B<\/strong> (\u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u043e\u0432) \u043a\u043b\u0435\u0442\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443. \u0420\u0443\u0431\u0435\u0436 \u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u0430 \u043f\u0440\u043e\u0439\u0434\u0435\u043d!<\/p>\n<h2>7. \u0423\u0431\u0438\u0440\u0430\u0435\u043c \u0433\u043b\u0430\u0432\u043d\u044b\u0439 IF<\/h2>\n<p>\u0420\u0430\u0437 \u044d\u0442\u043e \u0442\u0430\u043a \u043f\u043e\u043c\u043e\u0433\u043b\u043e, \u0442\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0438\u0437\u0431\u0430\u0432\u0438\u043c\u0441\u044f \u043e\u0442 \u0433\u043b\u0430\u0432\u043d\u043e\u0433\u043e IF, \u0437\u0430\u043c\u0435\u043d\u0438\u0432 \u0435\u0433\u043e lookup &#8212; \u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439. \u0417\u0430\u043e\u0434\u043d\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0441\u0442\u0430\u043d\u0435\u043c \u043c\u043d\u043e\u0433\u043e \u043a\u043e\u0440\u043e\u0447\u0435:<\/p>\n<pre><code class=\"julia\">function step6(size, board, helper)   #                                no cell  born           | live   2  3   4   lookup = zeros(Int8, 18).= [0, 0, 0, 1, 0, 0, 0, 0, 0,      0, 0, 1, 1, 0, 0, 0, 0, 0]   Threads.@threads for x in 1:size     left = ((x == 1) ? size : x-1)     lower = size     right = ((x == size) ? 1 : x+1)     for y in 1:size       upper = ((y == size) ? 1 : y+1)       # get the number of neighbours       @inbounds nb = board[upper, left] + board[upper, x] + board[upper, right] +                  board[y, left] +                            board[y, right] +                  board[lower, left] + board[lower, x]      + board[lower, right]       @inbounds cell = board[y, x]   @inbounds helper[y, x] = lookup[ cell*9 + nb+1]       lower = y     end   end      # now move modifications back   Threads.@threads for x in 1:size     for y in 1:size       @inbounds board[y,x] = helper[y,x]     end   end      return end<\/code><\/pre>\n<p>1 threads: <strong>0.256231 seconds (14 allocations: 1.656 KiB)<\/strong><\/p>\n<p>12 threads: <strong>0.053782 seconds (151 allocations: 16.406 KiB)<\/strong><\/p>\n<p>\u041c\u044b \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 <strong>1.859B<\/strong> (\u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u043e\u0432) \u043a\u043b\u0435\u0442\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443<\/p>\n<h2>8. \u0418\u0437\u0431\u0430\u0432\u043b\u044f\u0435\u043c\u0441\u044f \u043e\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 IF<\/h2>\n<p>\u0423 \u043d\u0430\u0441 \u043e\u0441\u0442\u0430\u043b\u0438\u0441\u044c IF \u0432 \u0432\u0438\u0434\u0435 ? \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0433\u0440\u0430\u043d\u0438\u0446 \u0442\u043e\u0440\u0430. \u0412 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u044d\u0442\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u0430\u043a \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 \u043c\u043e\u0434\u0443\u043b\u044f\u043c\u0438, \u043d\u043e \u0443\u0432\u044b, \u044d\u0442\u043e \u043b\u0438\u0448\u044c \u0437\u0430\u043c\u0435\u0434\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u0434\u043e \u043b\u0438\u0431\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0437\u0430\u0441\u0435\u0435\u043d\u044b \u043d\u0443\u043b\u044f\u043c\u0438 \u0438 \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0432\u0441\u0435, \u043a\u0440\u043e\u043c\u0435 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u043b\u0438\u0431\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434 \u0434\u043b\u044f \u0440\u0430\u0441\u0447\u0435\u0442\u0430 \u0433\u0440\u0430\u043d\u0438\u0446.<\/p>\n<p>\u0414\u043b\u044f \u043a\u0440\u0430\u0442\u043a\u043e\u0441\u0442\u0438 \u043a\u043e\u0434\u0430 \u044f \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0441\u044f \u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0438:<\/p>\n<pre><code class=\"julia\">function step7(size, board, helper)   #                                no cell  born           | live   2  3   4   lookup = zeros(Int8, 18).= [0, 0, 0, 1, 0, 0, 0, 0, 0,      0, 0, 1, 1, 0, 0, 0, 0, 0]   sizem1 = size - 1   Threads.@threads for x in 2:sizem1     left = x-1     right = x+1     for y in 2:sizem1       upper = y+1       lower = y-1       # get the number of neighbours       @inbounds nb = board[upper, left] + board[upper, x] + board[upper, right] +                  board[y, left] +                            board[y, right] +                  board[lower, left] + board[lower, x]      + board[lower, right]       @inbounds cell = board[y, x]   @inbounds helper[y, x] = lookup[ cell*9 + nb+1]     end   end      # now move modifications back   Threads.@threads for x in 1:size     for y in 1:size       @inbounds board[y,x] = helper[y,x]     end   end      return end<\/code><\/pre>\n<p>1 threads: <strong>0.164568 seconds (14 allocations: 1.656 KiB)<\/strong><\/p>\n<p>12 threads: <strong>0.036595 seconds (151 allocations: 16.406 KiB)<\/strong><\/p>\n<p>\u041c\u044b \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 <strong>2.732B<\/strong> (\u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u043e\u0432) \u043a\u043b\u0435\u0442\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443<\/p>\n<h2>9. \u0412\u0435\u043a\u0442\u043e\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438<\/h2>\n<p>\u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u043f\u0430\u043a\u0435\u0442 LoopVectorization, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u0430\u043a\u0440\u043e<a class=\"mention\" href=\"\/users\/turbo\">@turbo<\/a>\u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u043a\u043e\u0434 \u0446\u0438\u043a\u043b\u0430 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0432\u0430\u0436\u043d\u043e:<\/p>\n<ul>\n<li>\n<p>\u0412 \u0446\u0438\u043a\u043b\u0435 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043d\u0438\u043a\u0430\u043a\u0438\u0445 if (\u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 ?)<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0446\u0438\u043a\u043b\u0430 \u0434\u043b\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u0432\u0438\u0441\u0435\u0442\u044c \u0434\u0440\u0443\u0433 \u043e\u0442 \u0434\u0440\u0443\u0433\u0430 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0430\u043c \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0430\u0440\u0430\u0441\u0442\u0430\u044e\u0449\u0435\u0433\u043e \u0438\u0442\u043e\u0433\u0430)<\/p>\n<\/li>\n<li>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u043b\u044e\u0431\u044b\u0435 \u0441\u044e\u0440\u043f\u0440\u0438\u0437\u044b<\/p>\n<\/li>\n<li>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u043b\u044e\u0431\u044b\u0435 IF \u0437\u0430\u043f\u0440\u0435\u0449\u0435\u043d\u044b, \u0442\u043e<a class=\"mention\" href=\"\/users\/inbounds\">@inbounds<\/a>\u043f\u043e\u0434\u0440\u0430\u0437\u0443\u043c\u0435\u0432\u0430\u0435\u0442\u0441\u044f \u0432\u0441\u0435\u0433\u0434\u0430<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"julia\">function step8(size, board, helper)   lookup = zeros(Int8, 18).= [0, 0, 0, 1, 0, 0, 0, 0, 0,      0, 0, 1, 1, 0, 0, 0, 0, 0]   sizem1 = size - 1   Threads.@threads for x in 2:sizem1     left = x-1     right = x+1     @turbo for y in 2:sizem1       upper = y+1       lower = y-1       nb = board[upper, left] + board[upper, x] + board[upper, right] +                  board[y, left] +                            board[y, right] +                  board[lower, left] + board[lower, x]      + board[lower, right]       cell = board[y, x]   helper[y, x] = lookup[ cell*9 + nb+1]     end   end      Threads.@threads for x in 1:size     @turbo for y in 1:size       board[y,x] = helper[y,x]     end   end end<\/code><\/pre>\n<p>1 threads: <strong>0.069448 seconds (14 allocations: 1.656 KiB)<\/strong><\/p>\n<p>12 threads: <strong>0.021376 seconds (154 allocations: 16.500 KiB)<\/strong><\/p>\n<p>\u041d\u043e \u0443\u0432\u044b! \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u043c. \u0412\u0441\u0435 \u0442\u0430\u043a\u0438 \u043f\u0435\u0440\u0432\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0441\u043b\u043e\u0436\u043d\u044b\u043c \u0434\u043b\u044f \u0432\u0435\u043a\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438. \u0412\u0442\u043e\u0440\u043e\u0435<a class=\"mention\" href=\"\/users\/turbo\">@turbo<\/a>\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e, \u043d\u043e \u043d\u0435 \u0434\u0430\u0435\u0442 \u043f\u0440\u0438\u0440\u043e\u0441\u0442\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u041e\u0434\u043d\u0430\u043a\u043e \u0441\u0430\u043c\u0430 \u0438\u0434\u0435\u044f &#8216;\u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0449\u0438\u0445 \u043c\u0430\u043a\u0440\u043e\u0441\u043e\u0432&#8217; \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f!<\/p>\n<h2>10. \u0423\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u0435 \u0447\u0442\u0435\u043d\u0438\u0439<\/h2>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e, \u043a\u0430\u0436\u0434\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430 \u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f 9 \u0440\u0430\u0437. \u041c\u043e\u0436\u043d\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u044d\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0434\u043e \u0442\u0440\u0435\u0445, \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u0432 \u0441\u0432\u043e\u0435\u0431\u0440\u0430\u0437\u043d\u044b\u0439 &#8216;\u043a\u043e\u043d\u0432\u0435\u0439\u0435\u0440&#8217; \u0438\u0437 \u044f\u0447\u0435\u0435\u043a, \u0441\u043c\u0435\u0449\u0430\u044f \u0438\u0445 \u043d\u0430 \u043a\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u0438 \u0447\u0438\u0442\u0430\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b:<\/p>\n<pre><code class=\"julia\">function step9(size, board, helper)   lookup = zeros(Int8, 18).= [0, 0, 0, 1, 0, 0, 0, 0, 0,   0, 0, 1, 1, 0, 0, 0, 0, 0]   sizem1 = size - 1   Threads.@threads for x in 2:sizem1     left = x-1     right = x+1 cell_left_lower = cell_lower = cell_right_upper = 0 @inbounds cell_left = board[2, left] @inbounds cell_right = board[2, right] cell = board[2, x]     @inbounds for y in 2:sizem1       upper = y+1       lower = y-1   cell_left_upper = board[upper, left]   cell_right_upper = board[upper, right]   cell_upper = board[upper, x]       nb = cell_left_upper + cell_upper + cell_right_upper +               cell_left +           cell_right +               cell_left_lower + cell_lower + cell_right_upper   helper[y, x] = lookup[ cell*9 + nb+1]   # shift   cell_left_lower = cell_left   cell_lower = cell   cell_right_lower = cell_right   cell_left = cell_left_upper   cell = cell_upper   cell_right = cell_right_upper     end   end      Threads.@threads for x in 1:size     for y in 1:size       @inbounds board[y,x] = helper[y,x]     end   end end <\/code><\/pre>\n<p>\u041a\u0430\u043a \u043d\u0438 \u0441\u0442\u0440\u0430\u043d\u043d\u043e. \u0441\u0442\u0430\u043b\u043e \u0445\u0443\u0436\u0435:<\/p>\n<p>1 threads: <strong>0.188678 seconds (14 allocations: 1.656 KiB)<\/strong><\/p>\n<p>12 threads: <strong>0.040889 seconds (152 allocations: 16.438 KiB)<\/strong><\/p>\n<p>\u0410 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0438\u0434\u0435\u0438, \u043f\u043e\u0447\u0435\u043c\u0443 \u0441\u0442\u0430\u043b\u043e \u0445\u0443\u0436\u0435?<\/p>\n<h2>11. \u0425\u0440\u0430\u043d\u0438\u043c 8 \u043a\u043b\u0435\u0442\u043e\u043a \u0432 \u0431\u0430\u0439\u0442\u0435<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441\u0440\u0430\u0437\u0443 8 \u0431\u0438\u0442, \u0442\u043e \u0438\u0442\u043e\u0433\u043e\u0432\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 : \u0434\u0430\u043d\u043d\u044b\u0445 8 \u043a\u043b\u0435\u0442\u043e\u043a, 8 \u043a\u043b\u0435\u0442\u043e\u043a \u043d\u0430\u0434 \u0438 \u043f\u043e\u0434 \u043d\u0438\u043c\u0438 (\u0438\u0442\u043e\u0433\u043e 24), \u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0433\u0440\u0430\u043d\u0438\u0446 \u0441\u043b\u0435\u0432\u0430 \u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043f\u043e \u0442\u0440\u0438 \u043a\u043b\u0435\u0442\u043a\u0438, \u0447\u0442\u043e \u0434\u0430\u0435\u0442 30 \u0431\u0438\u0442 \u0438 lookup \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0432 1Gb &#8212; \u043d\u0435 \u0442\u0430\u043a \u043c\u043d\u043e\u0433\u043e \u043f\u043e \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c \u043c\u0435\u0440\u043a\u0430\u043c, \u043d\u043e \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0431\u0438\u0442\u043e\u0432\u0430\u044f \u043c\u0430\u0433\u0438\u044f:<\/p>\n<pre><code class=\"julia\">global size = 10000 # x global size8 = trunc(Int, size\/8) # y global board=zeros(UInt8, size8, size)  global helper=zeros(UInt8, size8, size) global lookup=zeros(UInt8, 256*256*256*64)  unction step10(size8, size, board, helper, lookup)   sizem1 = size - 1   size8m1 = size8 - 1   Threads.@threads for x in 2:sizem1     left = x-1     right = x+1 cell_left_lower = cell_lower = cell_right_upper = cell_right_lower = 0 @inbounds cell_left = board[2, left] @inbounds cell_right = board[2, right] cell = board[2, x]     @inbounds for y in 2:size8m1       upper = y+1       lower = y-1   cell_left_upper = board[upper, left]   cell_right_upper = board[upper, right]   cell_upper = board[upper, x]    helper[y, x] = lookup[cell | (cell_upper&lt;&lt;8) | (cell_lower&lt;&lt;16) +     ((cell_left_upper&amp;1)&lt;&lt;24) |          ((cell_left&amp;1)&lt;&lt;25) |         ((cell_left_lower&amp;1)&lt;&lt;26) |      # lower bit, &amp;1     ((cell_right_upper&amp;0x80)&lt;&lt;20) | ((cell_right&amp;0x80)&lt;&lt;21)          | ((cell_right_lower&amp;0x80)&lt;&lt;22) # high bit, &amp;127 ]   # shift   cell_left_lower = cell_left   cell_lower = cell   cell_right_lower = cell_right   cell_left = cell_left_upper   cell = cell_upper   cell_right = cell_right_upper     end   end      Threads.@threads for x in 1:size     for y in 1:size8       @inbounds board[y,x] = helper[y,x]     end   end end <\/code><\/pre>\n<p>1 threads: <strong>0.015258 seconds (12 allocations: 1.375 KiB)<\/strong><\/p>\n<p>12 threads: <strong>0.003306 seconds (149 allocations: 16.125 KiB)<\/strong><\/p>\n<p>30.248B \u043a\u043b\u0435\u0442\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443. \u0412\u0430\u0443.<\/p>\n<h2>12. 32 \u0431\u0438\u0442\u0430 \u0441\u0440\u0430\u0437\u0443.<\/h2>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0441\u0440\u0430\u043f\u0437\u0443 32 \u0431\u0438\u0442\u0430, \u0443\u0439\u0434\u044f \u0432 \u0441\u043e\u0432\u0441\u0435\u043c \u0433\u043b\u0443\u0431\u043e\u043a\u0443\u044e \u0431\u0438\u0442\u043e\u0432\u0443\u044e \u043c\u0430\u0433\u0438\u044e:<\/p>\n<pre><code class=\"julia\">      helper[y,x] =          lookup[(cell&amp;0xFF) | ((cell_upper&amp;0xFF)&lt;&lt;8) | ((cell_lower&amp;0xFF)&lt;&lt;16) +       ((cell_left_upper&amp;1)&lt;&lt;24) | ((cell_left&amp;1)&lt;&lt;25) | ((cell_left_lower&amp;1)&lt;&lt;26) |         ((cell_upper&amp;0x100)&lt;&lt;19) | ((cell&amp;0x100)&lt;&lt;20) | ((cell_lower&amp;0x100)&lt;&lt;21)       ] |       (lookup[(cell&amp;0xFF00>>8) | ((cell_upper&amp;0xFF00)) | ((cell_lower&amp;0xFF00)&lt;&lt;8) +       ((cell_upper&amp;0x80)&lt;&lt;17) | ((cell_left&amp;0x80)&lt;&lt;18) | ((cell_lower&amp;0x80)&lt;&lt;19) |         ((cell_upper&amp;0x10000)&lt;&lt;12) | ((cell&amp;0x10000)&lt;&lt;13) | ((cell_lower&amp;0x10000)&lt;&lt;14)       ] >> 8) |       (lookup[(cell&amp;0xFF0000>>16) | ((cell_upper&amp;0xFF0000)>>8) | ((cell_lower&amp;0xFF0000)) +       ((cell_upper&amp;0x8000)&lt;&lt;9) | ((cell_left&amp;0x8000)&lt;&lt;10) | ((cell_lower&amp;0x8000)&lt;&lt;11) |         ((cell_upper&amp;0x1000000)&lt;&lt;4) | ((cell&amp;0x1000000)&lt;&lt;5) | ((cell_lower&amp;0x1000000)&lt;&lt;6)       ] >> 16) |       (lookup[(cell&amp;0xFF000000>>24) | ((cell_upper&amp;0xFF000000)>>16) | ((cell_lower&amp;0xFF000000)>>8) +       ((cell_upper&amp;0x800000)&lt;&lt;1) | ((cell_left&amp;0x800000)&lt;&lt;2) | ((cell_lower&amp;0x800000)&lt;&lt;3) |         ((cell_right_upper&amp;0x80000000)>>4) | ((cell_right&amp;80000000)>>3) | ((cell_right_lower&amp;80000000)>>2)       ] >> 24) <\/code><\/pre>\n<p>\u041d\u043e \u044d\u0442\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0432 10 \u0440\u0430\u0437 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435&#8230;.\u0421\u043b\u0438\u0449\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u0431\u0438\u0442\u043e\u0432\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0434\u043b\u044f \u0443\u043f\u0430\u043a\u043e\u0432\u043a\u0438\/\u0440\u0430\u0441\u043f\u0430\u043a\u043e\u0432\u043a\u0438&#8230;<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/706228\/\"> https:\/\/habr.com\/ru\/post\/706228\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u042d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e\u0439 case \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438. \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043a\u0440\u0430\u0439\u043d\u0435 \u043f\u0440\u043e\u0441\u0442 \u0438 \u0435\u0433\u043e \u0437\u043d\u0430\u044e\u0442 \u0432\u0441\u0435. \u041d\u043e \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c!<\/p>\n<h2>1. Julia, \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u043f\u0435\u0440\u0432\u0430\u044f \u0438 \u043d\u0430\u0438\u0432\u043d\u0430\u044f<\/h2>\n<p>Julia \u0445\u043e\u0440\u043e\u0448\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442 \u0438\u0437 \u0441\u0435\u0431\u044f \u043f\u0438\u0442\u043e\u043d\u043e-\u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u044f\u0437\u044b\u043a \u0441 \u0443\u0442\u0438\u043d\u043e\u0439 \u0442\u0438\u043f\u0438\u0437\u0430\u0446\u0438\u0435\u0439, \u0431\u0443\u0434\u0443\u0447\u0438 \u043f\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u043d\u0435 \u043f\u0438\u0442\u043e\u043d\u043e \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0439. \u041f\u0440\u0438 \u0442\u043e\u043c \u0447\u0442\u043e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0443 \u043d\u0435\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u041f\u0438\u0442\u043e\u043d\u0430 (\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0442\u0441\u044f \u0441 1), \u043f\u043e\u0447\u0442\u0438 \u043a\u0430\u0436\u0434\u0443\u044e \u0441\u0442\u0440\u043e\u0447\u043a\u0443 \u043d\u0430\u0434\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c, \u043d\u043e \u0432\u0441\u0435 \u044d\u0442\u0438 \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u043d\u0438\u044f \u043a\u0430\u0436\u0443\u0442\u0441\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u044b\u043c\u0438, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0435\u0435 \u0445\u043e\u0440\u043e\u0448\u0438\u043c \u044f\u0437\u044b\u043a\u043e\u043c \u0434\u043b\u044f \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u043a\u043e\u0433\u0434\u0430 Python \u0443\u0436\u0435 \u0436\u043c\u0435\u0442. \u041d\u043e \u044d\u0442\u0430 \u043f\u043e\u0445\u043e\u0436\u0435\u0441\u0442\u044c \u043c\u043e\u0436\u0435\u0442 \u0441\u044b\u0433\u0440\u0430\u0442\u044c \u0434\u0443\u0440\u043d\u0443\u044e \u0441\u043b\u0443\u0436\u0431\u0443.<\/p>\n<p>\u041d\u0430\u0448\u0430 \u043f\u0435\u0440\u0432\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f:<\/p>\n<pre><code class=\"julia\">global size = 10000 global board=zeros(Int8, size, size)  global helper=zeros(Int8, size, size)  function step0()   global size, board, helper   for x in 1:size     left = ((x == 1) ? size : x-1)     lower = size     right = ((x == size) ? 1 : x+1)     for y in 1:size       upper = ((y == size) ? 1 : y+1)       # get the number of neighbours       nb = board[left, upper] + board[x, upper] + board[right, upper] +                  board[left, y] +                            board[right, y] +                  board[left, lower] + board[x, lower]      + board[right, lower]       cell = board[x, y]       w = 0       if (nb == 3) &amp;&amp; (cell == 0)          w = 1 # new born       elseif (nb &lt; 2) &amp;&amp; (cell == 1)          w = 1 # die lonely       elseif (nb > 3) &amp;&amp; (cell == 1)          w = 1 # die from owercrowding       end       helper[x, y] = w       lower = y     end   end      # now move modifications back   Threads.@threads for x in 1:size     for y in 1:size   if helper[x,y] > 0       board[x,y] = 1 - board[x,y]   end     end   end      return end <\/code><\/pre>\n<p>\u0414\u0430\u0435\u0442 \u0432\u0440\u0435\u043c\u044f <strong>85.486411 seconds (1.02 G allocations: 18.188 GiB, 1.83% gc time)<\/strong>. \u0427\u0442\u043e? \u041b\u0438\u0448\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 Python?<\/p>\n<p>\u041c\u044b \u0441\u043e\u0432\u0435\u0440\u0448\u0438\u043b\u0438 \u0441\u0442\u0440\u0430\u0448\u043d\u0443\u044e \u043e\u0448\u0438\u0431\u043a\u0443. \u041f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u043e\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e allocations. \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0432 Julia \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0438\u043c\u0435\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u0442\u0438\u043f. \u041f\u043e\u044d\u0442\u043e\u043c\u0443, \u043a\u043e\u0433\u0434\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435, \u0442\u043e \u043e\u043d\u0430 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0443\u0432\u0435\u0440\u0435\u043d\u0430 \u0432 \u0438\u0445 \u0442\u0438\u043f\u0435, \u0438 \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d\u0430 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 run time. \u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u043c \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0447\u0435\u0440\u0435\u0437 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:<\/p>\n<h2>2. \u041f\u0435\u0440\u0432\u0430\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<pre><code class=\"julia\">function step1(size, board, helper)   for x in 1:size     left = ((x == 1) ? size : x-1)     lower = size     right = ((x == size) ? 1 : x+1)     for y in 1:size       upper = ((y == size) ? 1 : y+1)       # get the number of neighbours       nb = board[left, upper] + board[x, upper] + board[right, upper] +                  board[left, y] +                            board[right, y] +                  board[left, lower] + board[x, lower]      + board[right, lower]       cell = board[x, y]       w = 0       if (nb == 3) &amp;&amp; (cell == 0)          w = 1 # new born       elseif (nb &lt; 2) &amp;&amp; (cell == 1)          w = 1 # die lonely       elseif (nb > 3) &amp;&amp; (cell == 1)          w = 1 # die from owercrowding       end       helper[x, y] = w       lower = y     end   end      # now move modifications back   for x in 1:size     for y in 1:size   if helper[x,y] > 0       board[x,y] = 1 - board[x,y]   end     end   end      return end<\/code><\/pre>\n<p><strong>1.882592 seconds<\/strong><\/p>\n<p>\u041d\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0439. \u0412\u043e\u0442 \u044d\u0442\u043e \u0443\u0436\u0435 \u0434\u0440\u0443\u0433\u043e\u0435 \u0434\u0435\u043b\u043e. \u041c\u044b \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 <strong>53M<\/strong> \u043a\u043b\u0435\u0442\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443 (\u043d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u044e, \u044d\u0442\u043e <strong>AMD Ryzen 5 3600X 6-Core Processor 3.79 GHz<\/strong>) <\/p>\n<h2>3. Multi-threading<\/h2>\n<p>\u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, \u0447\u0430\u0441\u0442\u043e \u0440\u0430\u0441\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u043d\u0430 Julia \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u043e &#8212; \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043c Threads<a class=\"mention\" href=\"\/users\/threads\">@threads<\/a>\u043f\u0435\u0440\u0435\u0434 \u0446\u0438\u043a\u043b\u0430\u043c\u0438. <\/p>\n<pre><code class=\"julia\">function step2(size, board, helper)   Threads.@threads for x in 1:size     left = ((x == 1) ? size : x-1)     lower = size     right = ((x == size) ? 1 : x+1)     for y in 1:size       upper = ((y == size) ? 1 : y+1)       # get the number of neighbours       nb = board[left, upper] + board[x, upper] + board[right, upper] +                  board[left, y] +                            board[right, y] +                  board[left, lower] + board[x, lower]      + board[right, lower]       cell = board[x, y]       w = 0       if (nb == 3) &amp;&amp; (cell == 0)          w = 1 # new born       elseif (nb &lt; 2) &amp;&amp; (cell == 1)          w = 1 # die lonely       elseif (nb > 3) &amp;&amp; (cell == 1)          w = 1 # die from owercrowding       end       helper[x, y] = w       lower = y     end   end      # now move modifications back   Threads.@threads for x in 1:size     for y in 1:size   if helper[x,y] > 0       board[x,y] = 1 - board[x,y]   end     end   end      return end <\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0442\u0435\u043c, \u0447\u0442\u043e \u0446\u0438\u043a\u043b\u044b \u043d\u0435 \u043f\u0435\u0440\u0435\u0441\u0435\u043a\u0430\u044e\u0442\u0441\u044f \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u043e \u0437\u0430\u043f\u0438\u0441\u0438. \u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0431\u0443\u0434\u0435\u043c \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u0442\u0440\u0435\u0434\u0435 \u0438 \u0441 <strong>&#8212;threads 12<\/strong>:<\/p>\n<p>1 thread: <strong>1.981022 seconds (12 allocations: 1.375 KiB)<\/strong><\/p>\n<p>12 threads: <strong>0.447547 seconds (124 allocations: 13.438 KiB)<\/strong><\/p>\n<p>\u041c\u044b \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 <strong>223M<\/strong> \u043a\u043b\u0435\u0442\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443<\/p>\n<h2>4. inbounds, \u043e\u0442\u043c\u0435\u043d\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430<\/h2>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u043c\u044b \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u0443\u0432\u0435\u0440\u0435\u043d\u044b \u0432 \u0441\u0435\u0431\u0435, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u044b\u043a\u043b\u044e\u0447\u0438\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438:<\/p>\n<pre><code class=\"julia\">function step3(size, board, helper)   Threads.@threads for x in 1:size     left = ((x == 1) ? size : x-1)     lower = size     right = ((x == size) ? 1 : x+1)     for y in 1:size       upper = ((y == size) ? 1 : y+1)       # get the number of neighbours       @inbounds nb = board[left, upper] + board[x, upper] + board[right, upper] +                  board[left, y] +                            board[right, y] +                  board[left, lower] + board[x, lower]      + board[right, lower]       @inbounds cell = board[x, y]       w = 0       if (nb == 3) &amp;&amp; (cell == 0)          w = 1 # new born       elseif (nb &lt; 2) &amp;&amp; (cell == 1)          w = 1 # die lonely       elseif (nb > 3) &amp;&amp; (cell == 1)          w = 1 # die from owercrowding       end       @inbounds helper[x, y] = w       lower = y     end   end      # now move modifications back   Threads.@threads for x in 1:size     for y in 1:size   @inbounds if helper[x,y] > 0       @inbounds board[x,y] = 1 - board[x,y]   end     end   end      return end<\/code><\/pre>\n<p>1 thread: <strong>1.584128 seconds (12 allocations: 1.375 KiB)<\/strong><\/p>\n<p>12 threads: <strong>0.348541 seconds (127 allocations: 13.531 KiB)<\/strong><\/p>\n<p>\u041c\u044b \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 <strong>286M<\/strong> \u043a\u043b\u0435\u0442\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443<\/p>\n<h2>5. Cache-friendly code<\/h2>\n<p>\u0410 \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u043f\u043e\u043c\u0435\u043d\u044f\u0435\u043c \u0432\u0435\u0437\u0434\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b x,y \u043d\u0430 y,x?<\/p>\n<pre><code class=\"julia\">function step4(size, board, helper)   Threads.@threads for x in 1:size     left = ((x == 1) ? size : x-1)     lower = size     right = ((x == size) ? 1 : x+1)     for y in 1:size       upper = ((y == size) ? 1 : y+1)       # get the number of neighbours       @inbounds nb = board[upper, left] + board[upper, x] + board[upper, right] +                  board[y, left] +                            board[y, right] +                  board[lower, left] + board[lower, x]      + board[lower, right]       @inbounds cell = board[y, x]       w = 0       if (nb == 3) &amp;&amp; (cell == 0)          w = 1 # new born       elseif (nb &lt; 2) &amp;&amp; (cell == 1)          w = 1 # die lonely       elseif (nb > 3) &amp;&amp; (cell == 1)          w = 1 # die from owercrowding       end       @inbounds helper[y, x] = w       lower = y     end   end      # now move modifications back   Threads.@threads for x in 1:size     for y in 1:size   @inbounds if helper[y,x] > 0       @inbounds board[y,x] = 1 - board[y,x]   end     end   end      return end<\/code><\/pre>\n<p>1 threads: <strong>0.842495 seconds (12 allocations: 1.375 KiB)<\/strong><\/p>\n<p>12 threads: <strong>0.113791 seconds (125 allocations: 13.469 KiB)<\/strong><\/p>\n<p>\u041c\u044b \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 <strong>879M<\/strong> \u043a\u043b\u0435\u0442\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443<\/p>\n<p>\u041d\u043e \u043f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e? \u0414\u0435\u043b\u043e \u0432 \u0443\u0440\u043e\u0432\u043d\u044f\u0445 \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438. \u041f\u0440\u0438 \u043e\u0431\u0445\u043e\u0434\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u043e\u0434\u0438\u043d \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430 \u043c\u043e\u043d\u043e\u0442\u043e\u043d\u043d\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442 \u0430\u0434\u0440\u0435\u0441 \u0434\u043e\u0441\u0442\u0443\u043f\u0430, \u0442\u043e\u0433\u0434\u0430 \u043a\u0430\u043a \u0434\u0440\u0443\u0433\u043e\u0439 \u0447\u0438\u0442\u0430\u0435\u0442 \u0431\u0430\u0439\u0442\u044b &#171;\u0432 \u0440\u0430\u0437\u0431\u0438\u0432\u043a\u0443&#187;. \u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e, \u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043c\u0430\u0442\u0440\u0438\u0446\u0430\u0445 \u044d\u0442\u043e \u0447\u043e\u0435\u043d\u044c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e.<\/p>\n<h2>6. \u0423\u0431\u0438\u0440\u0430\u0435\u043c \u043f\u0435\u0440\u0432\u044b\u0439 if<\/h2>\n<p>\u041e\u0442\u043c\u0435\u043d\u0430 \u043f\u0440\u0432\u0435\u0440\u043e\u043a \u0433\u0440\u0430\u043d\u0438\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u043d\u0430\u043c \u043f\u043e\u043c\u043e\u0433\u043b\u0430 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u0447\u0430\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0432\u044b\u0431\u0440\u043e\u0449\u0435\u043d\u043d\u043e\u0439, \u043d\u043e \u0438 \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043c\u044b \u0438\u0437\u0431\u0430\u0432\u0438\u043b\u0438\u0441\u044c \u043e\u0442 if, \u0430 \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0432\u0430\u0442\u044b \u0441\u0440\u044b\u0432\u043e\u043c \u043a\u043e\u043d\u0432\u0435\u0439\u0435\u0440\u0430. \u0415\u0441\u043b\u0438 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0433\u0440\u0430\u043d\u0438\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u043d\u0430 \u0438\u0437 \u0432\u0435\u0442\u043e\u043a \u043f\u043e\u0447\u0442\u0438 \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f, \u0442\u043e \u0432\u043e\u0442 \u044d\u0442\u043e\u0442 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"julia\">  if helper[y,x] > 0       board[y,x] = 1 - board[y,x]   end<\/code><\/pre>\n<p>\u0443\u0436\u0430\u0441\u0435\u043d. \u0443 \u044d\u0442\u043e\u0433\u043e IF \u043d\u0435\u0442 &#8216;\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0439&#8217; \u0441\u0442\u043e\u0440\u043e\u043d\u044b. \u043d\u043e \u043a\u0430\u043a \u0432\u043e\u043e\u0431\u0449\u0435 \u043c\u043d\u0435 \u043f\u0440\u0438\u0448\u043b\u0430 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u0438\u0434\u0435\u044f \u0432 helper \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u0437\u043d\u0430\u043a, \u0447\u0442\u043e \u043a\u043b\u0435\u0442\u043a\u0430 \u043f\u043e\u043c\u0435\u043d\u044f\u043b\u0430\u0441\u044c? \u041f\u0440\u043e\u0441\u0442\u043e \u044f DBA \u0438 \u043f\u043e\u0434\u0441\u043e\u0437\u043d\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b \u043e\u0431\u044a\u0435\u043c UPDATE. \u0410 \u044d\u0442\u043e\u0433\u043e \u0437\u0434\u0435\u0441\u044c \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u043d\u0430\u0434\u043e. \u0412 helper \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435:<\/p>\n<pre><code class=\"julia\">function step5(size, board, helper)   Threads.@threads for x in 1:size     left = ((x == 1) ? size : x-1)     lower = size     right = ((x == size) ? 1 : x+1)     for y in 1:size       upper = ((y == size) ? 1 : y+1)       # get the number of neighbours       @inbounds nb = board[upper, left] + board[upper, x] + board[upper, right] +                  board[y, left] +                            board[y, right] +                  board[lower, left] + board[lower, x]      + board[lower, right]       @inbounds cell = board[y, x]       w = cell       if (nb == 3) &amp;&amp; (cell == 0)          w = 1 # new born       elseif (nb &lt; 2) &amp;&amp; (cell == 1)          w = 0 # die lonely       elseif (nb > 3) &amp;&amp; (cell == 1)          w = 0 # die from owercrowding       end       @inbounds helper[y, x] = w       lower = y     end   end      # now move modifications back   Threads.@threads for x in 1:size     for y in 1:size       @inbounds board[y,x] = helper[y,x]     end   end      return end<\/code><\/pre>\n<p>1 threads: <strong>0.725642 seconds (12 allocations: 1.375 KiB)<\/strong><\/p>\n<p>12 threads: <strong>0.089742 seconds (148 allocations: 16.094 KiB)<\/strong><\/p>\n<p>\u041c\u044b \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 <strong>1.114B<\/strong> (\u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u043e\u0432) \u043a\u043b\u0435\u0442\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443. \u0420\u0443\u0431\u0435\u0436 \u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u0430 \u043f\u0440\u043e\u0439\u0434\u0435\u043d!<\/p>\n<h2>7. \u0423\u0431\u0438\u0440\u0430\u0435\u043c \u0433\u043b\u0430\u0432\u043d\u044b\u0439 IF<\/h2>\n<p>\u0420\u0430\u0437 \u044d\u0442\u043e \u0442\u0430\u043a \u043f\u043e\u043c\u043e\u0433\u043b\u043e, \u0442\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0438\u0437\u0431\u0430\u0432\u0438\u043c\u0441\u044f \u043e\u0442 \u0433\u043b\u0430\u0432\u043d\u043e\u0433\u043e IF, \u0437\u0430\u043c\u0435\u043d\u0438\u0432 \u0435\u0433\u043e lookup &#8212; \u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439. \u0417\u0430\u043e\u0434\u043d\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0441\u0442\u0430\u043d\u0435\u043c \u043c\u043d\u043e\u0433\u043e \u043a\u043e\u0440\u043e\u0447\u0435:<\/p>\n<pre><code class=\"julia\">function step6(size, board, helper)   #                                no cell  born           | live   2  3   4   lookup = zeros(Int8, 18).= [0, 0, 0, 1, 0, 0, 0, 0, 0,      0, 0, 1, 1, 0, 0, 0, 0, 0]   Threads.@threads for x in 1:size     left = ((x == 1) ? size : x-1)     lower = size     right = ((x == size) ? 1 : x+1)     for y in 1:size       upper = ((y == size) ? 1 : y+1)       # get the number of neighbours       @inbounds nb = board[upper, left] + board[upper, x] + board[upper, right] +                  board[y, left] +                            board[y, right] +                  board[lower, left] + board[lower, x]      + board[lower, right]       @inbounds cell = board[y, x]   @inbounds helper[y,<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\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-343108","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/343108","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=343108"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/343108\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=343108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=343108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=343108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}