{"id":255286,"date":"2015-04-15T15:29:05","date_gmt":"2015-04-15T11:29:05","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=255286"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=255286","title":{"rendered":"\u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0444\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u0430 \u0438\u043b\u0438 \u043c\u043e\u0449\u044c Stream API"},"content":{"rendered":"<p>     \t\u041d\u0430 \u0434\u043d\u044f\u0445 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0441\u0442\u0430\u0442\u044c\u044f  <a href=\"http:\/\/habrahabr.ru\/users\/5nw\/\" class=\"user_link\">5nw<\/a> <a href=\"http:\/\/habrahabr.ru\/post\/255761\/\">\u0414\u0432\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0444\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u0430<\/a>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0438\u0434\u0435\u044f \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u0441\u0447\u0451\u0442\u0430 \u0444\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u0435\u0440\u0435\u043c\u043d\u043e\u0436\u0430\u0435\u043c\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u0432 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443 \u00ab\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439\u00bb. \u0412\u0437\u0433\u043b\u044f\u043d\u0443\u0432 \u043d\u0430 \u044d\u0442\u043e, \u044f \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u043d\u044f\u043b, \u0447\u0442\u043e \u0442\u0443\u0442 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 Java \u043f\u0440\u043e\u044f\u0432\u044f\u0442 \u0441\u0435\u0431\u044f \u0432\u043e \u0432\u0441\u0435\u0439 \u043a\u0440\u0430\u0441\u0435: \u0432\u0435\u0434\u044c \u043e\u043d\u0438 \u0434\u0435\u043b\u044f\u0442 \u0437\u0430\u0434\u0430\u0447\u0443 \u043d\u0430 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043f\u043b\u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432 \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0435\u0449\u0451 \u0438 \u043a\u0440\u0430\u0441\u0438\u0432\u043e\u0439:<\/p>\n<pre><code class=\"java\">public static BigInteger streamedParallel(int n) {     if(n &lt; 2) return BigInteger.valueOf(1);     return IntStream.rangeClosed(2, n).parallel().mapToObj(BigInteger::valueOf).reduce(BigInteger::multiply).get(); }<\/code><\/pre>\n<p>  <a name=\"habracut\"><\/a><br \/>  \u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u0432 \u0441\u0442\u0430\u0442\u044c\u0435  <a href=\"http:\/\/habrahabr.ru\/users\/5nw\/\" class=\"user_link\">5nw<\/a> \u043d\u0435\u0442 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u0435\u0439 \u0437\u0430\u043c\u0435\u0440\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u0421\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0435\u0441\u0442\u043e\u0432 \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u043b\u043e\u0441\u044c? \u0411\u044b\u043b \u043b\u0438 \u0440\u0430\u0437\u043e\u0433\u0440\u0435\u0432? \u041e\u0446\u0435\u043d\u0438\u0432\u0430\u043b\u0430\u0441\u044c \u043b\u0438 \u043f\u043e\u0433\u0440\u0435\u0448\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u043c\u0435\u0440\u043e\u0432 \u0432\u0440\u0435\u043c\u0435\u043d\u0438? \u041d\u0435 \u0432\u044b\u043a\u043e\u0441\u0438\u043b \u043b\u0438 JIT-\u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0447\u0430\u0441\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0438\u0445 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c? \u0410 \u0435\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0432\u044b\u0432\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u0432 \u0444\u0430\u0439\u043b), \u0442\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0451\u043d \u043b\u0438 \u0444\u0430\u043a\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0438\u0437 \u043f\u043e\u0434\u0441\u0447\u0451\u0442\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u0438? \u0412 \u044d\u0442\u043e\u043c \u043f\u043b\u0430\u043d\u0435 \u0445\u043e\u0447\u0443 \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0441\u043f\u0430\u0441\u0438\u0431\u043e \u0410\u043b\u0435\u043a\u0441\u0435\u044e \u0428\u0438\u043f\u0438\u043b\u0451\u0432\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0432\u043e\u0435\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439 <a href=\"http:\/\/openjdk.java.net\/projects\/code-tools\/jmh\/\">JMH<\/a>, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043c\u043d\u043e\u0433\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u043f\u0440\u0435\u0437\u0435\u043d\u0442\u0430\u0446\u0438\u044f\u043c\u0438 \u0438 <a href=\"http:\/\/shipilev.net\/\">\u0441\u0442\u0430\u0442\u044c\u044f\u043c\u0438<\/a> \u043f\u0440\u0438\u0432\u0438\u043b \u043a\u0430\u043a\u0443\u044e-\u043d\u0438\u043a\u0430\u043a\u0443\u044e \u043a\u0443\u043b\u044c\u0442\u0443\u0440\u0443 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0438\u043d\u0433\u0430 \u0432 Java-\u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u0435.<\/p>\n<p>  \u0423 \u043c\u0435\u043d\u044f \u0431\u0443\u0434\u0435\u0442 \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430:  <\/p>\n<pre><code class=\"java\">import org.openjdk.jmh.infra.Blackhole; import org.openjdk.jmh.annotations.*; import java.util.concurrent.TimeUnit; import java.util.stream.IntStream; import java.math.BigInteger;  @Warmup(iterations=5) @Measurement(iterations=10) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.MICROSECONDS) @State(Scope.Benchmark) @Fork(2) public class Factorial {     @Param({&quot;10&quot;, &quot;100&quot;, &quot;1000&quot;, &quot;10000&quot;, &quot;50000&quot;})     private int n;      public static BigInteger naive(int n) {         BigInteger r = BigInteger.valueOf(1);         for (int i = 2; i &lt;= n; ++i)             r = r.multiply(BigInteger.valueOf(i));         return r;     }      public static BigInteger streamed(int n) {         if(n &lt; 2) return BigInteger.valueOf(1);         return IntStream.rangeClosed(2, n).mapToObj(BigInteger::valueOf).reduce(BigInteger::multiply).get();     }          public static BigInteger streamedParallel(int n) {         if(n &lt; 2) return BigInteger.valueOf(1);         return IntStream.rangeClosed(2, n).parallel().mapToObj(BigInteger::valueOf).reduce(BigInteger::multiply).get();     }      @Benchmark         public void testNaive(Blackhole bh) {         bh.consume(naive(n));     }          @Benchmark         public void testStreamed(Blackhole bh) {         bh.consume(streamed(n));     }          @Benchmark         public void testStreamedParallel(Blackhole bh) {         bh.consume(streamedParallel(n));     } }<\/code><\/pre>\n<p>  \u042f \u0441\u0440\u0430\u0432\u043d\u0438\u043b \u0442\u0440\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u2014 \u043d\u0430\u0438\u0432\u043d\u0443\u044e, \u043d\u0430 \u043e\u0431\u044b\u0447\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u0438 \u043d\u0430 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435. \u0420\u0430\u0437\u0443\u043c\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0434\u043b\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 n \u2014 10, 100, 1000, 10000 \u0438 50000, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0434\u0438\u043d\u0430\u043c\u0438\u043a\u0443 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432. \u041f\u0440\u043e\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u044f\u0442\u044c \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u0440\u0430\u0437\u043e\u0433\u0440\u0435\u0432\u0430 \u0438 \u0434\u0435\u0441\u044f\u0442\u044c \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u0441 \u0437\u0430\u043c\u0435\u0440\u043e\u043c, \u0432\u0441\u0451 \u044d\u0442\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f \u0434\u0432\u0430\u0436\u0434\u044b (\u0441 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u043a\u043e\u043c Java-\u043c\u0430\u0448\u0438\u043d\u044b), \u0442\u043e \u0435\u0441\u0442\u044c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u0435\u0441\u0442\u0430 \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f 20 \u0437\u0430\u043c\u0435\u0440\u043e\u0432. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0447\u0451\u0440\u043d\u0443\u044e \u0434\u044b\u0440\u0443 (Blackhole): \u043e\u043d\u0430 \u043d\u0443\u0436\u043d\u0430, \u0447\u0442\u043e\u0431\u044b JIT-\u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043d\u0435 \u0443\u0434\u0430\u043b\u0438\u043b \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439, \u0440\u0435\u0448\u0438\u0432, \u0447\u0442\u043e \u043e\u043d \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f.<\/p>\n<p>  \u041f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043b \u044f \u043d\u0430 \u043d\u043e\u0443\u0442\u0431\u0443\u043a\u0435 \u0441 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u043e\u043c Core i7-4702MQ (8 \u0445\u0430\u0440\u0434\u0432\u0430\u0440\u043d\u044b\u0445 \u0442\u0440\u0435\u0434\u043e\u0432). \u041f\u043e\u043b\u0443\u0447\u0430\u044e\u0442\u0441\u044f \u0432\u043e\u0442 \u0442\u0430\u043a\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b:<\/p>\n<pre><code class=\"java\">Benchmark                         (n)  Mode  Cnt       Score       Error  Units Factorial.testNaive                10  avgt   20       0.298 \u00b1     0.005  us\/op Factorial.testNaive               100  avgt   20       5.113 \u00b1     0.195  us\/op Factorial.testNaive              1000  avgt   20     278.601 \u00b1     9.586  us\/op Factorial.testNaive             10000  avgt   20   32232.618 \u00b1   889.512  us\/op Factorial.testNaive             50000  avgt   20  972369.158 \u00b1 29287.950  us\/op Factorial.testStreamed             10  avgt   20       0.339 \u00b1     0.021  us\/op Factorial.testStreamed            100  avgt   20       5.432 \u00b1     0.246  us\/op Factorial.testStreamed           1000  avgt   20     268.366 \u00b1     4.859  us\/op Factorial.testStreamed          10000  avgt   20   30429.526 \u00b1   435.611  us\/op Factorial.testStreamed          50000  avgt   20  896719.207 \u00b1  7995.599  us\/op Factorial.testStreamedParallel     10  avgt   20       6.470 \u00b1     0.515  us\/op Factorial.testStreamedParallel    100  avgt   20      11.280 \u00b1     1.094  us\/op Factorial.testStreamedParallel   1000  avgt   20      74.174 \u00b1     2.647  us\/op Factorial.testStreamedParallel  10000  avgt   20    2826.643 \u00b1    52.831  us\/op Factorial.testStreamedParallel  50000  avgt   20   49196.070 \u00b1   464.634  us\/op<\/code><\/pre>\n<p>  \u041d\u0430\u0438\u0432\u043d\u044b\u0439 \u0442\u0435\u0441\u0442 \u0432 \u0446\u0435\u043b\u043e\u043c \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0430\u0435\u0442 \u0442\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043e \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430. \u0420\u0430\u0437\u0434\u0435\u043b\u0438\u043c \u0432\u0440\u0435\u043c\u044f \u043d\u0430 n\u00b2:<\/p>\n<pre><code class=\"java\">n = 10:    0.002980 n = 100:   0.000511 n = 1000:  0.000279 n = 10000: 0.000322 n = 50000: 0.000389<\/code><\/pre>\n<p>  \u041f\u0440\u0438\u0440\u043e\u0441\u0442 \u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u0445, \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e, \u0441\u0432\u044f\u0437\u0430\u043d \u0441 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0435\u043c \u0440\u0430\u0441\u0445\u043e\u0434\u0430 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u0430\u043a\u0442\u0438\u0432\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u0441\u0431\u043e\u0440\u0449\u0438\u043a\u0430 \u043c\u0443\u0441\u043e\u0440\u0430.<\/p>\n<p>  \u041d\u0435\u0440\u0430\u0441\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0434\u043b\u044f \u043c\u0430\u043b\u044b\u0445 n \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u043e\u043b\u044c\u0448\u0435\u0435 \u0432\u0440\u0435\u043c\u044f (\u043d\u0430 13% \u0434\u043b\u044f n = 10 \u0438 \u043d\u0430 6% \u0434\u043b\u044f n = 100): \u0432\u0441\u0451 \u0436\u0435 \u043e\u0431\u0432\u044f\u0437\u043a\u0430 Stream API \u0447\u0442\u043e-\u0442\u043e \u0441\u044a\u0435\u0434\u0430\u0435\u0442. \u041e\u0434\u043d\u0430\u043a\u043e \u0443\u0434\u0438\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0447\u0442\u043e \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0445 n \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430 4-8% \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0445\u043e\u0442\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a \u0436\u0435. \u041e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0435 \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u0435 \u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u043f\u0430\u0441\u043d\u043e \u0440\u0430\u0441\u0441\u0443\u0436\u0434\u0430\u0442\u044c \u0442\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u043d\u0435 \u043f\u0440\u043e\u0432\u043e\u0434\u044f \u0437\u0430\u043c\u0435\u0440\u044b. \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 JIT-\u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430, \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430, \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0435 \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u043f\u0440\u043e\u0447\u0438\u0435 \u0444\u0430\u043a\u0442\u043e\u0440\u044b \u043e\u0447\u0435\u043d\u044c \u0442\u0440\u0443\u0434\u043d\u043e \u0443\u0447\u0435\u0441\u0442\u044c \u0432 \u0442\u0435\u043e\u0440\u0438\u0438.<\/p>\n<p>  \u0420\u0430\u0441\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435 \u0434\u043b\u044f \u043e\u0447\u0435\u043d\u044c \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439. \u041e\u0434\u043d\u0430\u043a\u043e \u043f\u0440\u0438\u0440\u043e\u0441\u0442 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u0442\u0441\u044f \u0443\u0436\u0435 \u043f\u0440\u0438 n = 1000, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u043d\u044b\u0439 \u0440\u0430\u0441\u0447\u0451\u0442 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u043e\u043a\u043e\u043b\u043e 270 \u043c\u043a\u0441 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435 \u0438 \u0442\u043e\u043b\u044c\u043a\u043e 75 \u0432 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u043c. \u042d\u0442\u043e \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u0441\u043e\u0433\u043b\u0430\u0441\u0443\u0435\u0442\u0441\u044f \u0441\u043e <a href=\"http:\/\/gee.cs.oswego.edu\/dl\/html\/StreamParallelGuidance.html\">Stream Parallel Guidance<\/a> (\u0441\u043f\u0430\u0441\u0438\u0431\u043e  <a href=\"http:\/\/habrahabr.ru\/users\/apangin\/\" class=\"user_link\">apangin<\/a> \u0437\u0430 \u0441\u0441\u044b\u043b\u043a\u0443), \u0433\u0434\u0435 \u0441\u043a\u0430\u0437\u0430\u043d\u043e, \u0447\u0442\u043e \u0440\u0430\u0441\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0432\u0430\u0442\u044c \u0438\u043c\u0435\u0435\u0442 \u0441\u043c\u044b\u0441\u043b \u0437\u0430\u0434\u0430\u0447\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0434\u043e\u043b\u044c\u0448\u0435 100 \u043c\u043a\u0441. \u041f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0436\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u0445 n \u0440\u0430\u0441\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u043d\u0430 \u043a\u043e\u043d\u0435: \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u0440\u0438\u0440\u043e\u0441\u0442 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u0432 18 \u0440\u0430\u0437. \u0412 \u0446\u0435\u043b\u043e\u043c \u044d\u0442\u043e \u0441\u043e\u0433\u043b\u0430\u0441\u0443\u0435\u0442\u0441\u044f \u0441 \u043f\u0440\u0438\u0440\u043e\u0441\u0442\u043e\u043c \u0443  <a href=\"http:\/\/habrahabr.ru\/users\/5nw\/\" class=\"user_link\">5nw<\/a>, \u043f\u043e\u043c\u043d\u043e\u0436\u0435\u043d\u043d\u044b\u043c \u043d\u0430 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 (1.7\/0.8*8 = 17).<\/p>\n<p>  \u0423 ForkJoinPool \u043e\u0447\u0435\u043d\u044c \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0439 \u043e\u0432\u0435\u0440\u0445\u0435\u0434, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u0430\u0436\u0435 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0432\u044b\u0438\u0433\u0440\u044b\u0448 \u043f\u043e \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0432\u0438\u0434\u0430 \u00ab\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439\u00bb \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043f\u0435\u0440\u0435\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438, \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0447\u0435\u043c\u0443 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u043c\u043e\u0436\u0435\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e. Stream API \u043c\u043d\u043e\u0433\u0438\u0435 \u0440\u0443\u0433\u0430\u044e\u0442, \u043d\u043e \u0437\u0430 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u043e\u043c \u0432\u0441\u0451 \u0436\u0435 \u0431\u0443\u0434\u0443\u0449\u0435\u0435.     \t<\/p>\n<div class=\"clear\"><\/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\/255813\/\"> http:\/\/habrahabr.ru\/post\/255813\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>     \t\u041d\u0430 \u0434\u043d\u044f\u0445 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0441\u0442\u0430\u0442\u044c\u044f  <a href=\"http:\/\/habrahabr.ru\/users\/5nw\/\" class=\"user_link\">5nw<\/a> <a href=\"http:\/\/habrahabr.ru\/post\/255761\/\">\u0414\u0432\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0444\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u0430<\/a>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0438\u0434\u0435\u044f \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u0441\u0447\u0451\u0442\u0430 \u0444\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u0435\u0440\u0435\u043c\u043d\u043e\u0436\u0430\u0435\u043c\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u0432 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443 \u00ab\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439\u00bb. \u0412\u0437\u0433\u043b\u044f\u043d\u0443\u0432 \u043d\u0430 \u044d\u0442\u043e, \u044f \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u043d\u044f\u043b, \u0447\u0442\u043e \u0442\u0443\u0442 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 Java \u043f\u0440\u043e\u044f\u0432\u044f\u0442 \u0441\u0435\u0431\u044f \u0432\u043e \u0432\u0441\u0435\u0439 \u043a\u0440\u0430\u0441\u0435: \u0432\u0435\u0434\u044c \u043e\u043d\u0438 \u0434\u0435\u043b\u044f\u0442 \u0437\u0430\u0434\u0430\u0447\u0443 \u043d\u0430 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043f\u043b\u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432 \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0435\u0449\u0451 \u0438 \u043a\u0440\u0430\u0441\u0438\u0432\u043e\u0439:<\/p>\n<pre><code class=\"java\">public static BigInteger streamedParallel(int n) {     if(n &lt; 2) return BigInteger.valueOf(1);     return IntStream.rangeClosed(2, n).parallel().mapToObj(BigInteger::valueOf).reduce(BigInteger::multiply).get(); }<\/code><\/pre>\n<p>  <\/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-255286","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/255286","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=255286"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/255286\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=255286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=255286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=255286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}