File: struct_vs_ostruct.rb

package info (click to toggle)
yard 0.9.37-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,720 kB
  • sloc: ruby: 31,354; javascript: 7,608; makefile: 21
file content (23 lines) | stat: -rw-r--r-- 1,002 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
require 'benchmark'
require 'ostruct'
require_relative '../lib/yard'

n = 100000
class MyStruct < Struct.new(:a, :b, :c); end
ostruct = OpenStruct.new
yostruct = YARD::OpenStruct.new
mystruct = MyStruct.new

Benchmark.bmbm do |x|
  x.report("Struct.new(args)") { n.times { MyStruct.new 1, 2, 3 } }
  x.report("Struct (assign)") { n.times { mystruct.a = 1 } }
  x.report("Struct (read)") { n.times { mystruct.a } }
  x.report("OpenStruct.new(args)") { n.times { OpenStruct.new a: 1, b: 2, c: 3 } }
  x.report("OpenStruct.new (blank)") { n.times { OpenStruct.new } }
  x.report("OpenStruct (assign)") { n.times { ostruct.a = 1 } }
  x.report("OpenStruct (read)") { n.times { ostruct.a } }
  x.report("YARD::OpenStruct.new(args)") { n.times { YARD::OpenStruct.new a: 1, b: 2, c: 3 } }
  x.report("YARD::OpenStruct.new (blank)") { n.times { YARD::OpenStruct.new } }
  x.report("YARD::OpenStruct (assign)") { n.times { yostruct.a = 1 } }
  x.report("YARD::OpenStruct (read)") { n.times { yostruct.a } }
end