Skip to content

Instantly share code, notes, and snippets.

@kernigh
Created October 12, 2021 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kernigh/32aef9ae4d9d4f6273f8df299675f87c to your computer and use it in GitHub Desktop.
Save kernigh/32aef9ae4d9d4f6273f8df299675f87c to your computer and use it in GitHub Desktop.
Ruby benchmark: 10 million Float to Integer
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