Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created May 3, 2022 00:14
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 havenwood/90664996049d251e67f86d485f9601d8 to your computer and use it in GitHub Desktop.
Save havenwood/90664996049d251e67f86d485f9601d8 to your computer and use it in GitHub Desktop.
A port of the Crystal "skynet" code to Ruby, see https://bugs.ruby-lang.org/issues/18760
require 'async'
require 'async/queue'
def skynet(c, num, size, div)
if size == 1
c << num
else
rc = Async::LimitedQueue.new(div)
sum = 0
div.times do |i|
sub_num = num + (i * (size / div))
Async { skynet(rc, sub_num, size / div, div) }
end
div.times do
sum += rc.dequeue
end
c << sum
end
end
c = Async::LimitedQueue.new(1)
start_time = Time.now
Sync { skynet(c, 0, 1_000_000, 10) }
result = Sync { c.dequeue }
end_time = Time.now
puts "Result: #{result} in #{end_time - start_time}s."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment