3

Why is Arrays.setAll faster than Arrays.parallelSetAll in this case?

int[] array = new int[30000000];
Random rnd = new Random();
Arrays.parallelSetAll(array, i->rnd.nextInt(200));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Azzabi Haythem
  • 2,318
  • 7
  • 26
  • 32
  • What is your question about then? – lucasvw Nov 17 '17 at 17:17
  • 4
    _"My question is not about @benchmark "_ -- Any question about Java performance MUST take into account everything known about how the JIT compiler works. You have not demonstrated that you have written a proper microbenchmark. If you believe you have done a proper microbenchmark then share everything you've done. – Jim Garrison Nov 17 '17 at 17:21
  • 1
    i dont used @Benchmark , but when i asked this question before it was blocked , and they confuse it with other question of how to use benchmark, but i will edit my question with bechmark – Azzabi Haythem Nov 17 '17 at 17:23
  • my question is not about using bechmark , why it's marked as duplicated with other subject !!!!!!!!!!!!!!!! – Azzabi Haythem Nov 17 '17 at 17:32
  • 2
    It is a good question - @Matthew McPeak answer below reinforces this - that it is not about benchmarking per se, but rather on internal issues (threading and contention in this case) that need to be enlightened about by developers with experience. Upping you one. – mohsenmadi Nov 17 '17 at 17:33
  • 1
    I don't know if it's a "good" question according to SO guidelines. But it is certainly a question about programming and it is not a duplicate question about how to write micro-benchmarks in Java. This question should be re-opened. – Matthew McPeak Nov 17 '17 at 17:46
  • 4
    Redirecting to “How do I write a correct micro-benchmark in Java?” is appropriate when there are actual indicators for flaws in the measuring method, but I do not agree that everyone has to prove the correctness of the measuring method. – Holger Nov 17 '17 at 20:06

1 Answers1

9

From the javadoc:

Instances of java.util.Random are threadsafe. However, the concurrent use of the same java.util.Random instance across threads may encounter contention and consequent poor performance. Consider instead using ThreadLocalRandom in multithreaded designs.

Matthew McPeak
  • 17,705
  • 2
  • 27
  • 59