Created
February 8, 2015 14:32
-
-
Save MartinHeimbring/ad28bfd9e35adb13e634 to your computer and use it in GitHub Desktop.
Using the Benchmark module to compare lookup time in hash
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' | |
| stringAZ = Hash[("a".."z").to_a.zip((1..26).to_a)] | |
| symbolAZ = Hash[(:a..:z).to_a.zip((1..26).to_a)] | |
| string_time = Benchmark.realtime do | |
| 100_000.times{stringAZ["r"]} | |
| end | |
| symbol_time = Benchmark.realtime do | |
| 100_000.times{symbolAZ[:r]} | |
| end | |
| puts "String time: #{string_time} seconds." | |
| puts "Symbol time: #{symbol_time} seconds." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment