Skip to content

Instantly share code, notes, and snippets.

@oeloeloel
Created August 3, 2021 18:09
Show Gist options
  • Save oeloeloel/174b2a302de96f255792c177ba85c71b to your computer and use it in GitHub Desktop.
Save oeloeloel/174b2a302de96f255792c177ba85c71b to your computer and use it in GitHub Desktop.
Modification of KFischer's Array Splat Performance Test
class AnObject
attr_reader :a, :b, :c, :d, :e, :f
def initialize
@a = 1
@b = 1
@c = 1
@d = 1
@e = 1
@f = 1
end
def access_instance_vars
@a
@b
@c
@d
@e
@f
end
end
AStruct = Struct.new(:a, :b, :c, :d, :e, :f) do
def access_instance_vars
@a
@b
@c
@d
@e
@f
end
end
def tick(args)
args.gtk.log_level = :off
if args.inputs.keyboard.key_down.space
array_a = Array.new(10_000) { 5 }
array_b = Array.new(10_000) { 5 }
array_c = Array.new(10_000) { 5 }
array_d = Array.new(10_000) { 5 }
array_e = Array.new(10_000) { 5 }
array_f = Array.new(10_000) { 5 }
objs = Array.new(10_000) { AnObject.new }
structs = Array.new(10_000) { AStruct.new(1, 2, 3, 4, 5, 6) }
arrays = Array.new(10_000) { [1, 2, 3, 4, 5, 6] }
args.gtk.benchmark iterations: 5000, # number of iterations
# using_separate_arrays_accesses: lambda {
# index = 0
# while index < 10000
# array_a[index]
# array_b[index]
# array_c[index]
# array_d[index]
# array_e[index]
# array_f[index]
# index += 1
# end
# },
# using_struct_accessor: lambda {
# index = 0
# while index < 10000
# obj = structs[index]
# obj.a
# obj.b
# obj.c
# obj.d
# obj.e
# obj.f
# index += 1
# end
# },
# using_obj_accesso: lambda {
# index = 0
# while index < 10000
# obj = objs[index]
# obj.a
# obj.b
# obj.c
# obj.a
# obj.b
# obj.c
# index += 1
# end
# },
access_instance_vars_obj: lambda {
index = 0
while index < 10_000
objs[index].access_instance_vars
index += 1
end
},
each_send: lambda {
a, b, c, d, e, f = fn.each_send(objs, self, :access_instance_vars)
},
using_array_splat: lambda {
index = 0
while index < 10_000
a, b, c, d, e, f = arrays[index]
index += 1
end
},
fn_each: lambda {
fn.each(objs) { |obj| obj.access_instance_vars
}
}
end
end
def access_instance_vars(thing)
thing.access_instance_vars
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment