Last active
February 5, 2026 15:34
-
-
Save keymastervn/efdb940ccdd08ccc935003485e21144a to your computer and use it in GitHub Desktop.
This file contains hidden or 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' | |
| require 'ostruct' | |
| ## OpenStruct inline | |
| # cars = [] | |
| # time = Benchmark.measure do | |
| # 250_000.times do | |
| # car = OpenStruct.new | |
| # car.wheel = 4 | |
| # car.mileage = rand((1..100_000)) | |
| # cars << car | |
| # end | |
| # end | |
| # puts time | |
| # 2.870059 0.321230 3.191289 ( 3.191373) | |
| ## Struct | |
| # car_struct = Struct.new(:wheels, :mileage) | |
| # cars = [] | |
| # time = Benchmark.measure do | |
| # 250_000.times do | |
| # cars << car_struct.new(4, rand((1..100_000))) | |
| # end | |
| # end | |
| # puts time | |
| # 0.042372 0.001419 0.043791 ( 0.043788) | |
| ## Struct inline | |
| # cars = [] | |
| # time = Benchmark.measure do | |
| # 250_000.times do | |
| # car_struct = Struct.new(:wheels, :mileage) | |
| # cars << car_struct.new(4, rand((1..100_000))) | |
| # end | |
| # end | |
| # puts time | |
| # 0.929561 0.165006 1.094567 ( 1.098186) | |
| ## Hash | |
| # cars = [] | |
| # time = Benchmark.measure do | |
| # 250_000.times do | |
| # cars << { wheels: 4, mileage: rand((1..100_000)) } | |
| # end | |
| # end | |
| # puts time | |
| # 0.908825 0.139471 1.048296 ( 1.048432) | |
| ## Data (immutable struct, since 3.2) | |
| # car_data = Data.define(:wheels, :mileage) | |
| # cars = [] | |
| # time = Benchmark.measure do | |
| # 250_000.times do | |
| # cars << car_data.new(4, rand((1..100_000))) | |
| # end | |
| # end | |
| # puts time | |
| # 0.073115 0.002047 0.075162 ( 0.075214) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment