How does the time it takes Math.random() to run compare to that of simple arithmetic operation?

Daniel

In Java, how long relative to a simple arithmetic operation does it take Math.random() to generate a number? I am trying to randomly distribute objects in an ArrayList that already contains values such that a somewhat even, but not completely even distribution is created, and I am not sure if choosing a random index for every insertion point by using Math.random() is the best approach to take.

Clarification: the distribution of inserted objects is meant to be even enough that the values are not all concentrated in one area, but also uneven enough that the distribution is not predictable (if someone were to go through the values one by one, they would not be able to determine if the next value was going to be a newly inserted value by detecting a constant pattern).

apangin

Do not use Math.random. It relies on a global instance of java.util.Random that uses AtomicLong under the hood. Though the PRNG algorithm used in java.util.Random is pretty simple, the performance is mostly affected by the atomic CAS and the related cache-coherence traffic.

This can be particularly bad for multithread appliсations (like in this example), but has also a penalty even in a single-threaded case.

ThreadLocalRandom is always preferable to Math.random. It does not rely on atomic operations and does not suffer from contention. It only updates a thread-local state and uses a couple of arithmetic and bitwise operations.

Here is a JMH benchmark to compare the performance of Math.random() and ThreadLocalRandom.current().nextDouble() to a simple arithmetic operation.

package bench;

import org.openjdk.jmh.annotations.*;
import java.util.concurrent.ThreadLocalRandom;

@State(Scope.Thread)
public class RandomBench {
    double x = 1;

    @Benchmark
    public double multiply() {
        return x * Math.PI;
    }

    @Benchmark
    public double mathRandom() {
        return Math.random();
    }

    @Benchmark
    public double threadLocalRandom() {
        return ThreadLocalRandom.current().nextDouble();
    }
}

The results show that ThreadLocalRandom works in just a few nanoseconds, its performance is comparable to a simple arithmetic operation and perfectly scales in multithreaded environment unlike Math.random.

Benchmark                      Threads     Score     Error  Units
RandomBench.mathRandom            1       34.265 ±   1.709  ns/op
RandomBench.multiply              1        4.531 ±   0.108  ns/op
RandomBench.threadLocalRandom     1        8.322 ±   0.047  ns/op

RandomBench.mathRandom            2      366.589 ±  63.899  ns/op
RandomBench.multiply              2        4.627 ±   0.118  ns/op
RandomBench.threadLocalRandom     2        8.342 ±   0.079  ns/op

RandomBench.mathRandom            4     1328.472 ± 177.216  ns/op
RandomBench.multiply              4        4.592 ±   0.091  ns/op
RandomBench.threadLocalRandom     4        8.474 ±   0.157  ns/op

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How does the distribution of Math.random()*50 + Math.random()*20 compare to Math.random()*70?

From Dev

Performing a Simple Math Operation in Real Time with Runtime Data in Qualtrics

From Dev

Easiest way to test how much time an operation takes in Qt

From Dev

How does Math.random() EXACTLY work in Java?

From Dev

How to do a simple math with TIME in CASE inside CREATEVIEW

From Dev

How to do arithmetic operation in TDE

From Dev

In RSpec, how to determine the time each spec file takes to run?

From Dev

How to create a query that takes long time to run in PostgreSQL

From Dev

How can I streamline this code and reduce the time it takes to run?

From Dev

Perl pattern match and arithmetic operation at the same time

From Dev

arithmetic operation on time datatype sql server?

From Dev

Does in class member initialization takes place at compile time or run-time?

From Dev

Does in class member initialization takes place at compile time or run-time?

From Dev

How does Priority queue compare and store the values during the push operation?

From Dev

Simple Ruby math operation code meaning

From Dev

How to compare Math.random() method generated current value with previous value

From Dev

Message being retried when operation takes time

From Dev

How does Math.random() generate random numbers beyond it's "native" range?

From Dev

how does c compiler handle unsigned and signed integer? Why the assembly code for unsigned and signed arithmetic operation are the same?

From Dev

How does mentioning multiple arithmetic operation within two operands work in Java

From Dev

how does c compiler handle unsigned and signed integer? Why the assembly code for unsigned and signed arithmetic operation are the same?

From Dev

prolog how to use math operation

From Dev

Arithmetic Operation

From Dev

Java Math.random() How random is it?

From Dev

Performing a simple arithmetic operation using SSE (IA32 assembly)

From Dev

Performing a simple arithmetic operation using SSE (IA32 assembly)

From Dev

Matlab: arithmetic operation on columns inside a for loop (simple yet devious!)

From Dev

Simple selects takes so much time to execute

From Dev

How to handle Arithmetic operation OverflowException in F#?

Related Related

  1. 1

    How does the distribution of Math.random()*50 + Math.random()*20 compare to Math.random()*70?

  2. 2

    Performing a Simple Math Operation in Real Time with Runtime Data in Qualtrics

  3. 3

    Easiest way to test how much time an operation takes in Qt

  4. 4

    How does Math.random() EXACTLY work in Java?

  5. 5

    How to do a simple math with TIME in CASE inside CREATEVIEW

  6. 6

    How to do arithmetic operation in TDE

  7. 7

    In RSpec, how to determine the time each spec file takes to run?

  8. 8

    How to create a query that takes long time to run in PostgreSQL

  9. 9

    How can I streamline this code and reduce the time it takes to run?

  10. 10

    Perl pattern match and arithmetic operation at the same time

  11. 11

    arithmetic operation on time datatype sql server?

  12. 12

    Does in class member initialization takes place at compile time or run-time?

  13. 13

    Does in class member initialization takes place at compile time or run-time?

  14. 14

    How does Priority queue compare and store the values during the push operation?

  15. 15

    Simple Ruby math operation code meaning

  16. 16

    How to compare Math.random() method generated current value with previous value

  17. 17

    Message being retried when operation takes time

  18. 18

    How does Math.random() generate random numbers beyond it's "native" range?

  19. 19

    how does c compiler handle unsigned and signed integer? Why the assembly code for unsigned and signed arithmetic operation are the same?

  20. 20

    How does mentioning multiple arithmetic operation within two operands work in Java

  21. 21

    how does c compiler handle unsigned and signed integer? Why the assembly code for unsigned and signed arithmetic operation are the same?

  22. 22

    prolog how to use math operation

  23. 23

    Arithmetic Operation

  24. 24

    Java Math.random() How random is it?

  25. 25

    Performing a simple arithmetic operation using SSE (IA32 assembly)

  26. 26

    Performing a simple arithmetic operation using SSE (IA32 assembly)

  27. 27

    Matlab: arithmetic operation on columns inside a for loop (simple yet devious!)

  28. 28

    Simple selects takes so much time to execute

  29. 29

    How to handle Arithmetic operation OverflowException in F#?

HotTag

Archive