Clojure

ThreadLocalRandom instead of Math/random

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: Release 1.7
  • Fix Version/s: Backlog
  • Component/s: None
  • Labels:
  • Environment:
    Requires Java >=1.7!
  • Patch:
    Code
  • Approval:
    Vetted

Description

The standard Math.random() is thread-safe through being declared as a synchronized static method.

The patch uses java.util.concurrent.ThreadLocalRandom which actually seems to be two times faster than the ordinary Math.random() in a simple single threaded criterium.core/bench:

The reason I investigated the function at all was to be sure random-number generation was not a bottleneck when performance testing multithreaded load generation.

If necessary, one could of course make a conditional declaration (like in fj-reducers) based on the existence of the class java.util.concurrent.ThreadLocalRandom, if Clojure 1.7 is to be compatible with Java versions < 1.7

Activity

Linus Ericsson made changes -
Field Original Value New Value
Attachment threadlocalrandom0002.patch [ 12988 ]
Linus Ericsson made changes -
Attachment threadlocalrandom0001.patch [ 12986 ]
Linus Ericsson made changes -
Attachment rand-tests0001.patch [ 12989 ]
Linus Ericsson made changes -
Attachment threadlocalrandom0002.patch [ 12990 ]
Linus Ericsson made changes -
Attachment threadlocalrandom0002.patch [ 12990 ]
Linus Ericsson made changes -
Attachment rand-tests0001.patch [ 12989 ]
Linus Ericsson made changes -
Attachment randomtests0002.patch [ 12991 ]
Linus Ericsson made changes -
Attachment threadlocalrandom0002.patch [ 12988 ]
Linus Ericsson made changes -
Attachment threadlocalrandom0003.patch [ 12992 ]
Linus Ericsson made changes -
Attachment randomtests0002.patch [ 12991 ]
Linus Ericsson made changes -
Attachment threadlocalrandom0003.patch [ 12992 ]
Linus Ericsson made changes -
Attachment threadlocalrandom0004.patch [ 12993 ]
Linus Ericsson made changes -
Attachment randomtests0003.patch [ 12994 ]
Linus Ericsson made changes -
Linus Ericsson made changes -
Attachment randomtests0003.patch [ 12994 ]
Linus Ericsson made changes -
Attachment threadlocalrandom0004.patch [ 12993 ]
Linus Ericsson made changes -
Comment [ patch not sensitive to negative arguments! ]
Linus Ericsson made changes -
Comment [ maybe the patches are correct now. ]
Linus Ericsson made changes -
Comment [ Fixed: The .nextDouble n doesn't accept negative arguments.

The speed mean runtime is now 30 ns instead of 20 ns, but still quicker than the Math.random (40 ns).
]
Alex Miller made changes -
Approval Vetted [ 10003 ]
Fix Version/s Backlog [ 10035 ]
Labels enhancement math patch performance math performance

People

Vote (0)
Watch (4)

Dates

  • Created:
    Updated: