diff --git a/exercises/practice/parallel-letter-frequency/.docs/instructions.append.md b/exercises/practice/parallel-letter-frequency/.docs/instructions.append.md index 67e82194ee..28798a1cf2 100644 --- a/exercises/practice/parallel-letter-frequency/.docs/instructions.append.md +++ b/exercises/practice/parallel-letter-frequency/.docs/instructions.append.md @@ -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) diff --git a/exercises/practice/parallel-letter-frequency/parallel_letter_frequency_benchmark_test.rb b/exercises/practice/parallel-letter-frequency/parallel_letter_frequency_benchmark_test.rb new file mode 100644 index 0000000000..025fcd973a --- /dev/null +++ b/exercises/practice/parallel-letter-frequency/parallel_letter_frequency_benchmark_test.rb @@ -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