Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compare Array#dup on 1D array, versus Marshal.load(Marshal.dump(...)) on 2D array
require 'benchmark'
N = 100_000
SIZE = 10
array_1d = Array.new(SIZE * SIZE, 0)
array_2d = Array.new(SIZE) { Array.new(SIZE, 0) }
Benchmark.bm do |bm|
bm.report("1d.dup") { N.times { array_1d.dup } }
bm.report("2d.dup") { N.times { Marshal.load(Marshal.dump(array_2d)) } }
end
user system total real
1d.dup 0.030000 0.000000 0.030000 ( 0.029078)
2d.dup 2.540000 0.010000 2.550000 ( 2.554168)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.