Skip to content

Instantly share code, notes, and snippets.

@keithrbennett
Last active February 15, 2021 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithrbennett/18f10124354d62eb8ba5feafaa9b39dc to your computer and use it in GitHub Desktop.
Save keithrbennett/18f10124354d62eb8ba5feafaa9b39dc to your computer and use it in GitHub Desktop.
Simple Ractor test based on Koichi's example at https://bugs.ruby-lang.org/issues/17497
#!/usr/bin/env ruby
# Run in directory containing `compar.c`, e.g. from https://github.com/ruby/ruby/blob/master/compar.c.
require 'etc'
WORDS = Ractor.make_shareable File.readlines('/usr/share/dict/words').map(&:chomp).map(&:downcase).sort
TRY_COUNT = Etc.nprocessors
puts "Measuring first sequentially on main ractor and then with #{TRY_COUNT} ractors:\n\n"
def try
File.readlines(__dir__ + '/compar.c').each{|line|
line.split.map(&:downcase).select { |text|
WORDS.include? text
}
}
end
Warning[:experimental] = false
require 'benchmark'
Benchmark.bm{|x|
obs_1 = x.report{
TRY_COUNT.times{try}
}
obs_n = x.report{
TRY_COUNT.times.map{
Ractor.new{ try }
}.each(&:take)
}
puts "\n#{sprintf("%7.3f", obs_1.utime / obs_n.utime)} User time difference factor"
puts "#{sprintf("%7.3f", obs_1.real / obs_n.real) } Real time difference factor"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment