If you're converting some symbol with a method that assumes the string, than you have additional to_s -> method -> to_sym convertation
time ruby -e '100_000_000.times { "A".downcase }' # 6.70s user 0.06s system 93% cpu 7.254 total
time ruby -e '100_000_000.times { :A.downcase }' # 8.39s user 0.04s system 99% cpu 8.497 total
class X
attr_accessor :a, :b
def initialize
@a = 10
@b = 20
end
def call(el)
return true if el > a && el < b
return false
end
end
In this call method on every execution we call attr_reader to fetch a and b values
x = X.new
100_000_000.times do
x.call(19)
end
If we replace a and b with a constants on this benchmark we'll save about 1.5s!
~ % time ruby ruby.rb
ruby ruby.rb 6.01s user 0.03s system 98% cpu 6.109 total
~ % time ruby ruby.rb
ruby ruby.rb 6.01s user 0.03s system 96% cpu 6.244 total
~ % time ruby ruby.rb
ruby ruby.rb 6.01s user 0.03s system 98% cpu 6.103 total
~ % time ruby ruby.rb
ruby ruby.rb 7.43s user 0.02s system 99% cpu 7.529 total
~ % time ruby ruby.rb
ruby ruby.rb 7.41s user 0.03s system 98% cpu 7.565 total
~ % time ruby ruby.rb
ruby ruby.rb 7.44s user 0.02s system 98% cpu 7.539 total