Skip to content

Instantly share code, notes, and snippets.

@skatkov
Created February 9, 2017 10:49
Show Gist options
  • Save skatkov/9d39394de46c413e303913825df0ed39 to your computer and use it in GitHub Desktop.
Save skatkov/9d39394de46c413e303913825df0ed39 to your computer and use it in GitHub Desktop.
Testing immutable data structure called 'Values' -> https://github.com/tcrayford/values/
require 'ostruct'
require 'benchmark'
require 'values'
COUNT = 10_000_000
NAME = "Test Name"
EMAIL = "test@example.org"
class Person
attr_accessor :name, :email
end
Vp = Value.new(:name, :email)
Benchmark.bm(13) do |x|
x.report("Value:") do
COUNT.times do
Vp.new(NAME, EMAIL)
end
end
x.report("hash:") do
COUNT.times do
p = {name: NAME, email: EMAIL}
end
end
x.report("openstruct:") do
COUNT.times do
p = OpenStruct.new
p.name = NAME
p.email = EMAIL
end
end
x.report("struct:") do
PersonStruct = Struct.new(:name, :email)
COUNT.times do
p = PersonStruct.new
p.name = NAME
p.email = EMAIL
end
end
x.report("class:") do
COUNT.times do
p = Person.new
p.name = NAME
p.email = EMAIL
end
end
end
$ ruby benchmark.rb
user system total real
Value: 22.270000 0.280000 22.550000 ( 23.955640)
hash: 6.690000 0.250000 6.940000 ( 7.792043)
openstruct: 138.330000 1.930000 140.260000 (148.726408)
struct: 2.860000 0.040000 2.900000 ( 2.957147)
class: 2.410000 0.030000 2.440000 ( 2.571136)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment