Skip to content

Instantly share code, notes, and snippets.

@jamis
Created September 26, 2015 20:28
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 jamis/0b9e932a23f8b273a327 to your computer and use it in GitHub Desktop.
Save jamis/0b9e932a23f8b273a327 to your computer and use it in GitHub Desktop.
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