Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
huonw opened this issue Jul 29, 2014 · 3 comments · Fixed by #21059
Closed

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

huonw opened this issue Jul 29, 2014 · 3 comments · Fixed by #21059

Comments

@huonw
Copy link
Member

huonw commented Jul 29, 2014

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();
}
@huonw huonw added the A-docs label Jul 29, 2014
@steveklabnik
Copy link
Member

👍

@thestinger
Copy link
Contributor

This kind of thing wouldn't be an issue with a fast TLS implementation though. LLVM knows how to perform optimizations on real TLS.

@steveklabnik
Copy link
Member

What's the story with this today? the removal of libgreen means that TLS got redone, correct?

steveklabnik added a commit to steveklabnik/rust that referenced this issue Jan 12, 2015
alexcrichton added a commit to alexcrichton/rust that referenced this issue Jan 15, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants