Ruby Class vs Struct

后端 未结 5 815
滥情空心
滥情空心 2021-02-02 08:48

I have seen codebases using Structs to wrap around attributes and behavior inside a class. What is the difference between a Ruby Class and a Struct? And when should one be used

5条回答
  •  北荒
    北荒 (楼主)
    2021-02-02 09:34

    I'd like to to @sam_forgot suggested benchmark. The comparison is not very fair. Both class and struct, these days, support keyword arguments. Using keyword arguments on each has opposite effects, as you can see from my example struct's with keyword arguments performance is not that dramatically different from the class.

    require 'benchmark'
    
    REP=1000000
    
    SUser = Struct.new(:name, :age)
    SUserK = Struct.new(:name, :age, keyword_init: true)
    
    DATA = { name: "Harry", age: 75 }
    DATA2 = DATA.values
    
    class CUser
      attr_accessor :name, :age
      def initialize(name, age)
        @name = name
        @age = age
      end
    end
    
    class CUserK
      attr_accessor :name, :age
      def initialize(name:, age:)
        @name = name
        @age = age
      end
    end
    
    Benchmark.bmbm do |x|
      x.report 'Struct create and access, without keyword arguments' do
        REP.times do
          user = SUser.new(DATA)
          "#{user.name} - #{user.age}"
        end
      end
    
      x.report 'Struct create and access, with keyword arguments' do
        REP.times do
          user = SUserK.new(**DATA)
          "#{user.name} - #{user.age}"
        end
      end
    
      x.report 'Class create and access, without keyword arguments' do
        REP.times do
          user = CUser.new(*DATA2)
          "#{user.name} - #{user.age}"
        end
      end 
    
      x.report 'Class create and access, with keyword arguments' do
        REP.times do
          user = CUserK.new(DATA)
          "#{user.name} - #{user.age}"
        end
      end
    end
    
    Rehearsal ---------------------------------------------------------------------------------------
    Struct create and access, without keyword arguments   3.484609   0.011974   3.496583 (  3.564523)
    Struct create and access, with keyword arguments      0.965959   0.005543   0.971502 (  1.007738)
    Class create and access, without keyword arguments    0.624603   0.003999   0.628602 (  0.660931)
    Class create and access, with keyword arguments       0.901494   0.004926   0.906420 (  0.952149)
    ------------------------------------------------------------------------------ total: 6.003107sec
    
                                                              user     system      total        real
    Struct create and access, without keyword arguments   3.300488   0.010372   3.310860 (  3.339511)
    Struct create and access, with keyword arguments      0.876742   0.004354   0.881096 (  0.903551)
    Class create and access, without keyword arguments    0.553393   0.003962   0.557355 (  0.568985)
    Class create and access, with keyword arguments       0.831672   0.004811   0.836483 (  0.850224)
    

提交回复
热议问题