Skip to content

Instantly share code, notes, and snippets.

@gr33n7007h

gr33n7007h/rt.rb Secret

Created May 28, 2021 06:16
Show Gist options
  • Save gr33n7007h/e8b11421b2270f1fd30618bba04bb48e to your computer and use it in GitHub Desktop.
Save gr33n7007h/e8b11421b2270f1fd30618bba04bb48e to your computer and use it in GitHub Desktop.
Ractor test
#!/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