Skip to content

Instantly share code, notes, and snippets.

@kigster
Last active August 24, 2025 06:31
Show Gist options
  • Select an option

  • Save kigster/0edbc05439e509ff164671ba7afec759 to your computer and use it in GitHub Desktop.

Select an option

Save kigster/0edbc05439e509ff164671ba7afec759 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'etc'
require 'parallel'
input = ('a'..'z').map { |letter| [letter, letter] }.to_h
index = 0
N = 5_000_000
cpu_count = Etc.nprocessors.to_i
cpu_array = (0...cpu_count).to_a
work = ->(count) { count.times { input.map { |key, value| [key.to_sym, value] }.to_h } }
results = Parallel.map(cpu_array, in_processes: cpu_count) do |cpu|
Benchmark.bm do |x|
x.report("cpu #{'%2d' % cpu}") { work[N] }
end
end
# puts results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment