Created
May 3, 2022 00:14
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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