-
Notifications
You must be signed in to change notification settings - Fork 432
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
WeightedIndex: Make it possible to update a subset of weights #866
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
390dfa2
WeightedIndex: Make it possible to update a subset of weights
vks 7351358
Benchmark creation vs. modification of `WeightedIndex`
vks 8d846a3
WeightedIndex::update_weights: Correct comment
vks 304ba15
WeightedIndex::update_weights: More efficient implementation
vks ee6f334
WeightedIndex: Clean up benchmark
vks 5ef9bde
Avoid an unnecessary clone
vks 8c258dc
Simplify `WeightedIndex::update_weights`
vks 20ebbd9
Remove outdated comments
vks c9428a0
Fix `WeightedIndex` for `alloc` builds
vks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2019 Developers of the Rand project. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(test)] | ||
|
||
extern crate test; | ||
|
||
use test::Bencher; | ||
use rand::Rng; | ||
use rand::distributions::WeightedIndex; | ||
|
||
#[bench] | ||
fn weighted_index_creation(b: &mut Bencher) { | ||
let mut rng = rand::thread_rng(); | ||
let weights = [1u32, 2, 4, 0, 5, 1, 7, 1, 2, 3, 4, 5, 6, 7]; | ||
b.iter(|| { | ||
let distr = WeightedIndex::new(weights.to_vec()).unwrap(); | ||
rng.sample(distr) | ||
}) | ||
} | ||
|
||
#[bench] | ||
fn weighted_index_modification(b: &mut Bencher) { | ||
let mut rng = rand::thread_rng(); | ||
let weights = [1u32, 2, 3, 0, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7]; | ||
let mut distr = WeightedIndex::new(weights.to_vec()).unwrap(); | ||
b.iter(|| { | ||
distr.update_weights(&[(2, &4), (5, &1)]).unwrap(); | ||
rng.sample(&distr) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be worth adding
InvalidIndex
, except that it's a breaking change. Perhaps do so in a separate PR which we don't land until we start preparing the next Rand version?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I though about this as well. Will do once this is merged.