Created
October 12, 2021 01:49
-
-
Save kernigh/32aef9ae4d9d4f6273f8df299675f87c to your computer and use it in GitHub Desktop.
Ruby benchmark: 10 million Float to Integer
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' | |
require 'carray' | |
require 'etc' | |
Ractor.new {} # show "warning: Ractor is experimental..." | |
puts "Making an Array of Floats..." | |
array = 10_000_000.times.map{500 * rand} | |
array1, array2, array3 = nil | |
nproc = Etc.nprocessors | |
Benchmark.bmbm do |x| | |
x.report("Float#to_i") {array1 = array.map &:to_i} | |
x.report("CArray") {array2 = CA_DOUBLE(array1).int.to_a} | |
x.report("Ractor") do | |
size = array.size.quo(nproc).ceil # size / nproc rounded up | |
array3 = array.each_slice(size).map do |part| | |
Ractor.new(part) {|part| Ractor.yield part.map &:to_i} | |
end.flat_map &:take | |
end | |
end | |
puts "-- Used #{nproc} Ractors." | |
array1 == array2 or fail "CArray gave wrong answer" | |
array1 == array3 or fail "Ractor gave wrong answer" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment