Ad
  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Nop.
    I just run a benchmark to be sure, with smallNumber = 25 and bigNumber = Integer.MAX_SIZE.

    Here are the results :

    Benchmark Mode Cnt Score Error Units
    MultiplesOf3And5Test.withForLoopAndBigNumber avgt 5 2.680 ± 0.055 s/op
    MultiplesOf3And5Test.withForLoopAndSmallNumber avgt 5 28.495 ± 4.046 ns/op
    MultiplesOf3And5Test.withParallelStreamAndBigNumber avgt 5 1.044 ± 0.184 s/op
    MultiplesOf3And5Test.withParallelStreamAndSmallNumber avgt 5 11284.553 ± 1710.007 ns/op
    MultiplesOf3And5Test.withStreamAndBigNumber avgt 5 3.013 ± 0.049 s/op
    MultiplesOf3And5Test.withStreamAndSmallNumber avgt 5 94.385 ± 3.109 ns/op

    (Yes I used warmup steps to remove JIT and CPU bias)

    Here are my interpretations :

    • The withForLoop and the withStream are very close with small numbers. The difference comes from the stream initialisation.
    • The performance converge with BigNumber
    • The parallel implem with default thread-pool stream is 2 time better than every other
    • The parallel implem with custom thread-pool could be even better
    • People should stop throwing performance asumptions without proper benchmark
  • Custom User Avatar

    Nop, the text says "below n" and not "equals or below n".
    So for n=9, then 14 is the correct answer (3 + 5 + 6).

    Also, if you really want inclusive IntStream, use IntStream.rangeClosed(start, end)