Skip to content

Instantly share code, notes, and snippets.

@elhoyos
Last active December 31, 2015 20:39
Show Gist options
  • Select an option

  • Save elhoyos/8041778 to your computer and use it in GitHub Desktop.

Select an option

Save elhoyos/8041778 to your computer and use it in GitHub Desktop.
require 'benchmark'
# Constant vs String comparison benchmark
class AType; end
types = []
(1..10000).each { types.push(AType) }
time = Benchmark.realtime do
types.each { |t| AType == t }
end
puts "Constant elapsed: #{time*1000}ms"
strings = types.map { |t| t.name }
time = Benchmark.realtime do
strings.each { |s| "AType" == s }
end
puts "String elapsed: #{time*1000}ms"
Constant elapsed: 0.929ms
String elapsed: 4.665ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment