Created
August 4, 2023 18:45
-
-
Save Thomascountz/f9c8912c3b6aae0345f4b4d2901ec16c to your computer and use it in GitHub Desktop.
Ruby Hash v. Struct v. Data v. Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark/ips' | |
PointStruct = Struct.new(:x, :y, :grayscale) | |
PointData = Data.define(:x, :y, :grayscale) | |
class PointClass | |
attr_accessor :x, :y, :grayscale | |
def initialize(x, y, grayscale) | |
@x, @y, @grayscale = x, y, grayscale | |
end | |
end | |
Benchmark.ips do |x| | |
x.report("Creating Struct") { PointStruct.new(rand(100), rand(100), rand(256)) } | |
x.report("Creating Data") { PointData.new(rand(100), rand(100), rand(256)) } | |
x.report("Creating Class") { PointClass.new(rand(100), rand(100), rand(256)) } | |
x.report("Creating Hash") { { x: rand(100), y: rand(100), grayscale: rand(256) } } | |
x.compare! | |
end | |
Benchmark.ips do |x| | |
x.report("Accessing Struct") { point = PointStruct.new(rand(100), rand(100), rand(256)); point.x; point.y; point.grayscale } | |
x.report("Accessing Data") { point = PointData.new(rand(100), rand(100), rand(256)); point.x; point.y; point.grayscale } | |
x.report("Accessing Class") { point = PointClass.new(rand(100), rand(100), rand(256)); point.x; point.y; point.grayscale } | |
x.report("Accessing Hash") { point = { x: rand(100), y: rand(100), grayscale: rand(256) }; point[:x]; point[:y]; point[:grayscale] } | |
x.compare! | |
end | |
# Results: | |
# 2023-08-04 20:41:44 CET | |
# 2,6 GHz 6-Core Intel Core i7 | |
# Intel UHD Graphics 630 1536 MB | |
# 32 GB 2667 MHz DDR4 | |
# Warming up -------------------------------------- | |
# Creating Struct 180.269k i/100ms | |
# Creating Data 124.964k i/100ms | |
# Creating Class 175.496k i/100ms | |
# Creating Hash 220.985k i/100ms | |
# Calculating ------------------------------------- | |
# Creating Struct 1.893M (± 2.7%) i/s - 9.554M in 5.051606s | |
# Creating Data 1.259M (± 3.2%) i/s - 6.373M in 5.067984s | |
# Creating Class 1.781M (± 3.1%) i/s - 8.950M in 5.030144s | |
# Creating Hash 2.207M (± 3.2%) i/s - 11.049M in 5.012252s | |
# | |
# Comparison: | |
# Creating Hash: 2206856.5 i/s | |
# Creating Struct: 1892751.2 i/s - 1.17x (± 0.00) slower | |
# Creating Class: 1781183.8 i/s - 1.24x (± 0.00) slower | |
# Creating Data: 1258870.2 i/s - 1.75x (± 0.00) slower | |
# | |
# Warming up -------------------------------------- | |
# Accessing Struct 181.624k i/100ms | |
# Accessing Data 120.734k i/100ms | |
# Accessing Class 163.452k i/100ms | |
# Accessing Hash 195.814k i/100ms | |
# Calculating ------------------------------------- | |
# Accessing Struct 1.779M (± 3.2%) i/s - 8.900M in 5.008624s | |
# Accessing Data 1.142M (± 8.2%) i/s - 5.674M in 5.007587s | |
# Accessing Class 1.608M (± 4.6%) i/s - 8.173M in 5.095153s | |
# Accessing Hash 1.952M (± 3.0%) i/s - 9.791M in 5.020502s | |
# | |
# Comparison: | |
# Accessing Hash: 1951927.5 i/s | |
# Accessing Struct: 1778751.7 i/s - 1.10x (± 0.00) slower | |
# Accessing Class: 1607887.9 i/s - 1.21x (± 0.00) slower | |
# Accessing Data: 1141760.5 i/s - 1.71x (± 0.00) slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment