Created
July 24, 2013 05:18
-
-
Save JonathonMA/6068228 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
| def hash_smash bench_size, bm | |
| hash_1 = {} | |
| hash_2 = {} | |
| 1.upto(bench_size) do |n| | |
| k = "val#{n}" | |
| hash_1[k] = { | |
| name: k, | |
| total: n * 2, | |
| } | |
| end | |
| h1 = hash_1["val500"][:total] | |
| 1.upto(bench_size) do |n| | |
| k = "val#{n}" | |
| hash_2[k] = { | |
| name: k, | |
| total: n * 3, | |
| } | |
| end | |
| h2 = hash_2["val500"][:total] | |
| bm.report(bench_size) do | |
| hash_1.each do |k, v| | |
| v[:total] += hash_2[k][:total] | |
| end | |
| end | |
| h3 = hash_1["val500"][:total] | |
| abort "oops this is wrong" unless h3 == h1 + h2 | |
| end | |
| require 'benchmark' | |
| Benchmark.bm(10) do |bm| | |
| hash_smash 2_000, bm | |
| hash_smash 20_000, bm | |
| hash_smash 200_000, bm | |
| hash_smash 2_000_000, bm | |
| end | |
| __END__ | |
| user system total real | |
| 2000 0.000000 0.000000 0.000000 ( 0.000832) | |
| 20000 0.020000 0.000000 0.020000 ( 0.015878) | |
| 200000 0.200000 0.000000 0.200000 ( 0.197828) | |
| 2000000 2.770000 0.040000 2.810000 ( 2.818214) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment