Skip to content

Instantly share code, notes, and snippets.

@bbozo
Last active January 3, 2016 13:19
Show Gist options
  • Save bbozo/8468940 to your computer and use it in GitHub Desktop.
Save bbozo/8468940 to your computer and use it in GitHub Desktop.
getter_benchmark.rb
class Foo1
FOO = 3
end
class Foo2
def self.foo
3
end
end
time_start = Time.now
10000000.times{Foo1::FOO}
time_1 = Time.now
10000000.times{Foo2.foo}
time_2 = Time.now
puts "Constant: #{time_1 - time_start}"
puts "Class method: #{time_2 - time_1}"
bbozo@uhurajr-ubuntu:~/dev$ ruby getter.rb
Constant: 0.947
Class method: 0.495
bbozo@uhurajr-ubuntu:~/dev$ ruby getter.rb
Constant: 0.617
Class method: 0.505
bbozo@uhurajr-ubuntu:~/dev$ ruby getter.rb
Constant: 0.829
Class method: 0.552
bbozo@uhurajr-ubuntu:~/dev$ ruby getter.rb
Constant: 0.775749431
Class method: 1.081718486
bbozo@uhurajr-ubuntu:~/dev$ ruby getter.rb
Constant: 0.748656884
Class method: 1.117390897
bbozo@uhurajr-ubuntu:~/dev$ ruby getter.rb
Constant: 0.74989894
Class method: 1.05571574
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment