Last active
October 23, 2024 00:20
-
-
Save mauricioszabo/bb2b3b8db9baa5014924e0471aa636ed to your computer and use it in GitHub Desktop.
"Parallel Ruby" example
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 "benchmark" | |
# Only to avoid the warning below | |
Ractor.new {} if RUBY_PLATFORM != "java" | |
def weird_sum(a, b) | |
while(b > 0) | |
b -= 1 | |
a += 1 | |
end | |
a | |
end | |
TOTAL = 50_200_000 | |
Benchmark.bm do |b| | |
b.report('Direct Result') do | |
v1 = weird_sum(0, TOTAL) | |
v2 = weird_sum(0, TOTAL) | |
v3 = weird_sum(0, TOTAL) | |
v4 = weird_sum(0, TOTAL) | |
v5 = weird_sum(0, TOTAL) | |
v6 = weird_sum(0, TOTAL) | |
v7 = weird_sum(0, TOTAL) | |
v8 = weird_sum(0, TOTAL) | |
# Force result | |
[:total_direct, v1+v2+v3+v4+v5+v6+v7+v8] | |
end | |
b.report('Threads Result') do | |
v1 = Thread.new { weird_sum(0, TOTAL) } | |
v2 = Thread.new { weird_sum(0, TOTAL) } | |
v3 = Thread.new { weird_sum(0, TOTAL) } | |
v4 = Thread.new { weird_sum(0, TOTAL) } | |
v5 = Thread.new { weird_sum(0, TOTAL) } | |
v6 = Thread.new { weird_sum(0, TOTAL) } | |
v7 = Thread.new { weird_sum(0, TOTAL) } | |
v8 = Thread.new { weird_sum(0, TOTAL) } | |
# Force result | |
[:total_direct, v1.value+v2.value+v3.value+v4.value+v5.value+v6.value+v7.value+v8.value] | |
end | |
if RUBY_PLATFORM != "java" | |
b.report('Parallel Result') do | |
v1 = Ractor.new { Ractor.yield weird_sum(0, TOTAL) } | |
v2 = Ractor.new { Ractor.yield weird_sum(0, TOTAL) } | |
v3 = Ractor.new { Ractor.yield weird_sum(0, TOTAL) } | |
v4 = Ractor.new { Ractor.yield weird_sum(0, TOTAL) } | |
v5 = Ractor.new { Ractor.yield weird_sum(0, TOTAL) } | |
v6 = Ractor.new { Ractor.yield weird_sum(0, TOTAL) } | |
v7 = Ractor.new { Ractor.yield weird_sum(0, TOTAL) } | |
v8 = Ractor.new { Ractor.yield weird_sum(0, TOTAL) } | |
# Force result | |
[:total_direct, v1.take+v2.take+v3.take+v4.take+v5.take+v6.take+v7.take+v8.take] | |
end | |
end | |
end |
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
user system total real | |
Direct Result 3.250000 0.260000 3.510000 ( 3.255143) | |
Threads Result 13.980000 0.040000 14.020000 ( 1.773047) |
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
user system total real | |
Direct Result 8.726516 0.000000 8.726516 ( 8.726669) | |
Threads Result 8.296119 0.006886 8.303005 ( 8.297598) | |
Parallel Result 84.070994 0.000997 84.071991 ( 13.245050) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment