Skip to content

Docs for std::rand::random should discuss performance #16072

Closed
@huonw

Description

@huonw

std::rand::random is quite slow, since it has to do a TLD look-up to find the task_rng, and only then generate the random number. The docs should mention that if random is called repeatedly, one should do a single look-up via the task_rng() function, and then call the Rng methods directly.

E.g.

for x in foo.mut_iter() {
    *x = rand::random()
}

// would be faster as

let mut rng = rand::task_rng();
for x in foo.mut_iter() {
    *x = rng.gen();
}

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