-
-
Save gr33n7007h/e8b11421b2270f1fd30618bba04bb48e to your computer and use it in GitHub Desktop.
Ractor test
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
#!/usr/bin/env ruby | |
require 'benchmark' | |
require 'etc' | |
def fib(n) | |
return 1 if n <= 2 | |
k = n >> 1 | |
a = fib(k + 1) | |
b = fib(k) | |
n % 2 == 1 ? (a.abs2 + b.abs2) : (b * ((a << 1) - b)) | |
end | |
N = 10_000_000 | |
Benchmark.bm { | |
_1.report('seq') { | |
Etc.nprocessors.times { fib N } | |
} | |
_1.report('par') { | |
Etc.nprocessors.times.map { | |
Ractor.new { fib N } | |
}.each &:take | |
} | |
} | |
__END__ | |
user system total real | |
seq 5.381498 0.003257 5.384755 ( 5.394994) | |
par 23.197400 8.358632 31.556032 ( 8.293363) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment