File: speed.rb

package info (click to toggle)
ruby-ice-nine 0.11.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 432 kB
  • sloc: ruby: 1,006; makefile: 2
file content (34 lines) | stat: -rwxr-xr-x 681 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env ruby

# encoding: utf-8

# benchmark speed of deep freeze

$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)

require 'rbench'
require 'ice_nine'

# @return [Hash]
def self.nested(depth, width, array_length)
  hash = {}

  1.upto(width) do |n|
    hash[n.to_s] = n.to_s
  end

  unless depth == 1
    hash[(width - 1).to_s] = array_length.times.map { nested(depth - 1, width, array_length) }
    hash[width.to_s] = nested(depth - 1, width, array_length)
  end

  hash
end

hash = nested(3, 5, 500)
hash2 = nested(3, 5, 500)

RBench.run do
  report('deep_freeze')  { IceNine.deep_freeze(hash)   }
  report('deep_freeze!') { IceNine.deep_freeze!(hash2) }
end