Skip to content

Instantly share code, notes, and snippets.

@luke-gru
Created January 29, 2023 14:44
Show Gist options
  • Save luke-gru/adf7468fbc0a3807b5f9a875fcfce116 to your computer and use it in GitHub Desktop.
Save luke-gru/adf7468fbc0a3807b5f9a875fcfce116 to your computer and use it in GitHub Desktop.
JSON parse tight loop
require 'json'
require 'benchmark'
RACTORS = ARGV.first == "ractor"
J = { rand => rand }.to_json
Ractor.make_shareable(J)
iterations = 1_000_000
Benchmark.bmbm do |x|
x.report("json parse#{RACTORS ? ' (ractors)' : ''}") {
if RACTORS
rs = []
10.times.each do
rs << Ractor.new(iterations) do |iters|
i = 0
iters = iters / 10
while i < iters
JSON.parse(J)
i+=1
end
end
end
rs.each(&:take)
else
i = 0
while i < iterations
JSON.parse(J)
i+=1
end
end
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment