-
-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Minitest benchmarking file for sequential vs parallel execution
Co-authored-by: KOTP <[email protected]>
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
exercises/practice/parallel-letter-frequency/.docs/instructions.append.md
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 |
---|---|---|
@@ -1,7 +1,19 @@ | ||
# Instructions Append | ||
|
||
## Benchmarking | ||
|
||
If you are solving this exercise locally, there is an included `parallel_letter_frequency_benchmark_test.rb` file which will compare your parallel solution to a sequential one. | ||
You can run it via `ruby parallel_letter_frequency_benchmark_test.rb`. | ||
The output will show execution times of each implementation over a range of large text counts in a tabular format. | ||
Feel free to change the test cases. | ||
You may want to investigate what performance impact length of text vs number of texts has on the execution time for each implementation. | ||
|
||
**Note:** For smaller sets of text, the sequential count _may_ be faster due to processing overhead costs. | ||
|
||
## Further Reading | ||
|
||
- [Ruby `Thread` Documentation](https://docs.ruby-lang.org/en/master/Thread.html) | ||
- [Ruby `Thread::Queue` Documentation](https://docs.ruby-lang.org/en/master/Thread/Queue.html) | ||
- [Ruby `Fiber` Documentation](https://docs.ruby-lang.org/en/master/Fiber.html) | ||
- [Ruby `Ractor` Documentation](https://docs.ruby-lang.org/en/master/Ractor.html) | ||
- [`Minitest::Benchmark` Documentation](https://ruby-doc.org/3.0.6/gems/minitest/Minitest/Benchmark.html) |
39 changes: 39 additions & 0 deletions
39
exercises/practice/parallel-letter-frequency/parallel_letter_frequency_benchmark_test.rb
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,39 @@ | ||
require 'minitest/autorun' | ||
require 'minitest/benchmark' | ||
require_relative 'parallel_letter_frequency' | ||
|
||
class ParallelLetterFrequencyBenchmarkTest < Minitest::Benchmark | ||
def self.bench_range | ||
bench_exp(1, 100_000) | ||
end | ||
|
||
def setup | ||
@strings = {} | ||
self.class.bench_range.each do |n| | ||
@strings[n] = Array.new(10, 'a' * n) | ||
end | ||
end | ||
|
||
def bench_sequential | ||
assert_performance ->(_, _) { true } do |n| | ||
sequential_letter_frequency(@strings[n]) | ||
end | ||
end | ||
|
||
def bench_parallel | ||
assert_performance ->(_, _) { true } do |n| | ||
ParallelLetterFrequency.count(@strings[n]) | ||
end | ||
end | ||
|
||
def sequential_letter_frequency(texts) | ||
tally = Hash.new(0) | ||
texts.each do |text| | ||
text.each_grapheme_cluster do |cluster| | ||
tally[cluster] += 1 | ||
end | ||
end | ||
|
||
tally | ||
end | ||
end |