Skip to content

Instantly share code, notes, and snippets.

@ko1

ko1/bench.rb Secret

Created April 17, 2025 19:25
Show Gist options
  • Save ko1/5e8cb732a8d8ecfe49605674c5a01193 to your computer and use it in GitHub Desktop.
Save ko1/5e8cb732a8d8ecfe49605674c5a01193 to your computer and use it in GitHub Desktop.
Simple tasks on Ractors
Warning[:experimental] = false
$:.unshift File.join(__dir__, '.bundle/gems/benchmark-0.4.0/lib/')
require 'benchmark'
RN = ENV['RN'].to_i
raise if RN < 1
ON = (2 * 4 * 8 * 16) * 10_000
TN = ON/RN
raise({TN:TN, RN:RN, ON:ON}.inspect) if TN * RN != ON
type = ENV['T'].to_sym
case type
when :regexp
def task
str = 'hello world'
TN.times{
/(h)(e)(l)(l)(o)/ =~ str
}
end
when :new
def task
TN.times{
''
}
end
when :newary
def task
a = []
TN.times{
a = [a]
}
end
else
raise
end
Benchmark.bm(10){|x|
if RN == 1
x.report("#{type}/#{RN}"){
task
}
else
x.report("#{type}/#{RN}"){
rs = RN.times.map{
Ractor.new do
task
:finished
end
}
rs.map(&:take)
}
end
}
# p result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment