Created
September 6, 2025 08:36
-
-
Save KonnorRogers/8cada1b9b78b7efa060d11d98f3e92c8 to your computer and use it in GitHub Desktop.
Classes, Hashes, Structs, oh my.
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
def boot(args) | |
args.state = {} | |
end | |
SpriteStruct = Struct.new(:x, :y, :w, :h, :path) | |
class SpriteClass | |
attr_accessor :x, :y, :w, :h, :path | |
def initialize(x:, y:, w:, h:, path:) | |
@x = x | |
@y = y | |
@w = w | |
@h = h | |
@path = path | |
end | |
end | |
def tick args | |
if args.inputs.keyboard.key_down.b | |
args.gtk.console.show | |
args.gtk.benchmark( | |
iterations: 1000, # number of iterations | |
# label for experiment | |
creating_structs: lambda { | |
path = "foo" | |
5000.times do |i| | |
SpriteStruct.new(i, i, i, i, path) | |
end | |
}, | |
creating_classes: lambda { | |
path = "foo" | |
5000.times do |i| | |
SpriteClass.new(w: i, h: i, x: i, y: i, path: path) | |
end | |
}, | |
# label for experiment | |
creating_hashes: lambda { | |
path = "foo" | |
5000.times do |i| | |
{ x: i, y: i, w: i, h: i, path: path } | |
end | |
} | |
) | |
end | |
if args.inputs.keyboard.key_down.m | |
args.gtk.console.show | |
args.gtk.benchmark( | |
iterations: 1000, # number of iterations | |
mutating_hash_bracket: lambda { | |
path = "foo" | |
i = 0 | |
sprite = { x: i, y: i, w: i, h: i, path: path } | |
5000.times do |i| | |
sprite[:x] = i + 1 | |
end | |
}, | |
# label for experiment | |
mutating_hash_dot: lambda { | |
path = "foo" | |
i = 0 | |
sprite = { x: i, y: i, w: i, h: i, path: path } | |
5000.times do |i| | |
sprite.x = i + 1 | |
end | |
}, | |
mutating_structs: lambda { | |
path = "foo" | |
sprite = SpriteStruct.new(0, 0, 0, 0, path) | |
5000.times do |i| | |
sprite.x = i + 1 | |
end | |
}, | |
mutating_classes: lambda { | |
path = "foo" | |
sprite = SpriteClass.new(w: 0, h: 0, x: 0, y: 0, path: path) | |
5000.times do |i| | |
sprite.x = i + 1 | |
end | |
}, | |
) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment