Closed amomchilov closed 3 years ago
Could you merge this? Only you have access
Reflecting on this, I was curious about using the bind_call
trick to translate the method over from Kernel. Turns out it's a bit slower:
#!/usr/bin/ruby
# Calculating -------------------------------------
# #singleton_class 30.646M (± 1.2%) i/s - 155.427M in 5.072506s
# bind_call 9.594M (± 4.7%) i/s - 48.233M in 5.043490s
# class << 20.887M (± 1.1%) i/s - 104.728M in 5.014629s
require "benchmark/ips"
o = Object.new
SINGLETON_CLASS_METHOD = Kernel.instance_method(:singleton_class)
Benchmark.ips do |x|
x.config(warmup: 0.1, time: 5)
x.report("#singleton_class") do |times|
i = 0
while (i += 1) < times
o.singleton_class
end
end
x.report("bind_call") do |times|
i = 0
while (i += 1) < times
SINGLETON_CLASS_METHOD.bind_call(o)
end
end
x.report("class <<") do |times|
i = 0
while (i += 1) < times
class << o; self; end
end
end
end
Clarify what the
(class << object; self; end)
trick does.I also split apart the large comment into what is (IMO) a more readable form. WDYT?