Constant elapsed: 0.929ms
String elapsed: 4.665ms
Last active
December 31, 2015 20:39
-
-
Save elhoyos/8041778 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment