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
Optimize Indexable#sample(n, random) #10247
Draft
HertzDevil
wants to merge
1
commit into
crystal-lang:master
Choose a base branch
from
HertzDevil:feature/indexable-sample
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Optimize Indexable#sample(n, random) #10247
HertzDevil
wants to merge
1
commit into
crystal-lang:master
from
HertzDevil:feature/indexable-sample
Conversation
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
Sija
reviewed
Jan 13, 2021
@@ -571,13 +571,25 @@ module Indexable(T) | |||
|
|||
# :nodoc: | |||
def sample(n : Int, random = Random::DEFAULT) | |||
return super unless n == 1 | |||
# Unweighted reservoir sampling (Algorithm L): |
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.
Suggested change
# Unweighted reservoir sampling (Algorithm L): | |
return [] of T if empty? | |
# Unweighted reservoir sampling (Algorithm L): |
straight-shoota
approved these changes
Jan 13, 2021
Do you have benchmarks for this? |
Fixture: require "benchmark"
module Indexable(T)
def sample_old(n : Int, random = Random::DEFAULT)
if n != 1
# copied from Enumerable#sample
ary = Array(T).new(n)
return ary if n == 0
each_with_index do |elem, i|
if i < n
ary << elem
else
j = random.rand(i + 1)
if j < n
ary.to_unsafe[j] = elem
end
end
end
return ary.shuffle!(random)
end
if empty?
[] of T
else
[sample(random)]
end
end
end
# ARY_SIZE is selected from 10, 100, 1000, 10000
# N is selected from 2, 10, ARY_SIZE / 2
ary = Array(Int32).new(ENV["ARY_SIZE"].to_i) { 0 }
count = ENV["N"].to_i
puts "Sampling #{count} elements from #{ary.class} with size #{ary.size}"
rng = Random::DEFAULT
Benchmark.ips do |x|
x.report("sample_old") do
1000.times { ary.sample_old(count, rng) }
end
x.report("sample") do
1000.times { ary.sample(count, rng) }
end
end Results:
A few takeaways here:
Maybe we should employ some kind of heuristic here to select with algorithm to use. That requires a lot more research. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Implements Algorithm L for multiple-element sampling, which reduces the time complexity from
O(size)
toO(k(1 + log(size / k)))
. This requires the ability to skip multiple elements at once, which is only doable inIndexable
but notEnumerable
.I personally think we may expose
Random#rand_exclusive
later. It is required here because the algorithm will overflow if#rand
returns exactly 0.0.