Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MartinHeimbring/ad28bfd9e35adb13e634 to your computer and use it in GitHub Desktop.
Save MartinHeimbring/ad28bfd9e35adb13e634 to your computer and use it in GitHub Desktop.

Revisions

  1. MartinHeimbring created this gist Feb 8, 2015.
    15 changes: 15 additions & 0 deletions Benchmark: Lookup time (string vs.symbol as key)
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    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."