Sha256: 0ba39acc89166922c6ab9ec2495bdbdb2dda1eedb3623f27722e00ab3bb3fd25

Contents?: true

Size: 448 Bytes

Versions: 3

Compression:

Stored size: 448 Bytes

Contents

require 'benchmark'

# Point = Struct.new :x, :y
class Point
  attr_accessor :x, :y
  def initialize(x,y)
    @x = x
    @y = y
  end
end
NUM = 10_000_000
Benchmark.bm(60) do |b|
  b.report("array") do
    NUM.times do 
      it = [4,6]
      it[0] = 1
      it[1] = 3
      it[0]
      it[1]
    end
  end
  b.report("struct") do
    NUM.times do 
      it = Point.new 4, 6
      it.x = 1
      it.y = 3
      it.x 
      it.y 
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gamebox-0.3.4 script/perf_struct_vs_array.rb
gamebox-0.3.3 script/perf_struct_vs_array.rb
gamebox-0.3.2 script/perf_struct_vs_array.rb