Con respecto a los comentarios sobre la velocidad de uso de Hashes, Struct o OpenStruct: Hash siempre ganará para uso general. Es la base de OpenStruct sin la formación adicional de hielo, por lo que no es tan flexible, pero es delgado y malo.
Uso de Ruby 2.4.1:
require 'fruity'
require 'ostruct'
def _hash
h = {}
h['a'] = 1
h['a']
end
def _struct
s = Struct.new(:a)
foo = s.new(1)
foo.a
end
def _ostruct
person = OpenStruct.new
person.a = 1
person.a
end
compare do
a_hash { _hash }
a_struct { _struct }
an_ostruct { _ostruct }
end
# >> Running each test 4096 times. Test will take about 2 seconds.
# >> a_hash is faster than an_ostruct by 13x ± 1.0
# >> an_ostruct is similar to a_struct
Usando definiciones más concisa de los hash y OpenStruct:
require 'fruity'
require 'ostruct'
def _hash
h = {'a' => 1}
h['a']
end
def _struct
s = Struct.new(:a)
foo = s.new(1)
foo.a
end
def _ostruct
person = OpenStruct.new('a' => 1)
person.a
end
compare do
a_hash { _hash }
a_struct { _struct }
an_ostruct { _ostruct }
end
# >> Running each test 4096 times. Test will take about 2 seconds.
# >> a_hash is faster than an_ostruct by 17x ± 10.0
# >> an_ostruct is similar to a_struct
Si la estructura, Hash o Struct o OpenStruct se define una vez y luego utilizado muchas veces , entonces la velocidad de acceso se vuelve más importante, y una Struct comienza a brillar:
require 'fruity'
require 'ostruct'
HSH = {'a' => 1}
def _hash
HSH['a']
end
STRCT = Struct.new(:a).new(1)
def _struct
STRCT.a
end
OSTRCT = OpenStruct.new('a' => 1)
def _ostruct
OSTRCT.a
end
puts "Ruby version: #{RUBY_VERSION}"
compare do
a_hash { _hash }
a_struct { _struct }
an_ostruct { _ostruct }
end
# >> Ruby version: 2.4.1
# >> Running each test 65536 times. Test will take about 2 seconds.
# >> a_struct is faster than a_hash by 4x ± 1.0
# >> a_hash is similar to an_ostruct
Sin embargo, tenga en cuenta que el Struct es solo 4 veces más rápido que el Hash para acceder, mientras que el Hash es 17 veces más rápido para la inicialización, la asignación y el acceso. Tendrá que descubrir cuál es el mejor para usar en función de las necesidades de una aplicación en particular.Tiendo a usar Hashes para uso general como resultado.
Además, la velocidad de uso de OpenStruct ha mejorado enormemente a lo largo de los años; Lo que solía ser más lento que Struct basado en puntos de referencia que he visto en el pasado y que comparan a 1.9.3-P551:
require 'fruity'
require 'ostruct'
def _hash
h = {}
h['a'] = 1
h['a']
end
def _struct
s = Struct.new(:a)
foo = s.new(1)
foo.a
end
def _ostruct
person = OpenStruct.new
person.a = 1
person.a
end
puts "Ruby version: #{RUBY_VERSION}"
compare do
a_hash { _hash }
a_struct { _struct }
an_ostruct { _ostruct }
end
# >> Ruby version: 1.9.3
# >> Running each test 4096 times. Test will take about 2 seconds.
# >> a_hash is faster than a_struct by 7x ± 1.0
# >> a_struct is faster than an_ostruct by 2x ± 0.1
y:
require 'fruity'
require 'ostruct'
def _hash
h = {'a' => 1}
h['a']
end
def _struct
s = Struct.new(:a)
foo = s.new(1)
foo.a
end
def _ostruct
person = OpenStruct.new('a' => 1)
person.a
end
puts "Ruby version: #{RUBY_VERSION}"
compare do
a_hash { _hash }
a_struct { _struct }
an_ostruct { _ostruct }
end
# >> Ruby version: 1.9.3
# >> Running each test 4096 times. Test will take about 2 seconds.
# >> a_hash is faster than a_struct by 7x ± 1.0
# >> a_struct is faster than an_ostruct by 2x ± 1.0
y:
require 'fruity'
require 'ostruct'
HSH = {'a' => 1}
def _hash
HSH['a']
end
STRCT = Struct.new(:a).new(1)
def _struct
STRCT.a
end
OSTRCT = OpenStruct.new('a' => 1)
def _ostruct
OSTRCT.a
end
puts "Ruby version: #{RUBY_VERSION}"
compare do
a_hash { _hash }
a_struct { _struct }
an_ostruct { _ostruct }
end
# >> Ruby version: 1.9.3
# >> Running each test 32768 times. Test will take about 1 second.
# >> a_struct is faster than an_ostruct by 3x ± 1.0
# >> an_ostruct is similar to a_hash
Consideraría una estructura más fácil de entender, es decir, que conduce a un código más fácil de mantener. Dejaré que otra persona haga comentarios sobre las ventajas de rendimiento. –
También vale la pena señalar que las estructuras superan a Hashes en términos de velocidad de recuperación de datos, lo que las convierte en mejores opciones para cualquier configuración a la que se deba acceder repetidamente durante el tiempo de ejecución. – user2398029
Consulte la sección "Estructura vs. OpenStruct vs. Hash" en [Estructura de adentro hacia afuera] (http://blog.rubybestpractices.com/posts/rklemme/017-Struct.html). –