Skip to content

ThreadRng performance bug #43120

@SharplEr

Description

@SharplEr

ThreadRng works 5 times slower than ThreadLocalRandom in Java.

I run this benchmark in Rust:

#[bench]
fn bench_rnd(b: &mut Bencher) {
    b.iter(|| rand::thread_rng().gen_range::<f64>(2.0, 100.0));
}

On my laptop the result is:
test tests::bench_rnd ... bench: 49 ns/iter (+/- 1)

But if I run the same benchmark on JHM:

@Benchmark
public double testRnd() {
    return ThreadLocalRandom.current().nextDouble(2, 100);
}

On my laptop the result is:
Benchmark Mode Cnt Score Error Units
Main.testRnd avgt 20 9,018 ± 0,094 ns/op

So the difference is 5.44 times looks like performance bug.

Meta

Rust:

rbose
rustc 1.20.0-nightly (c9bb935 2017-06-24)
binary: rustc
commit-hash: c9bb935
commit-date: 2017-06-24
host: x86_64-unknown-linux-gnu
release: 1.20.0-nightly
LLVM version: 4.0

Java:
OpenJDK 1.8.131

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions