Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Created June 17, 2019 09:31
Show Gist options
  • Save genkuroki/8e5d266cd0c4708d123359e39d10670f to your computer and use it in GitHub Desktop.
Save genkuroki/8e5d266cd0c4708d123359e39d10670f to your computer and use it in GitHub Desktop.
Dict test
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using BenchmarkTools",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function ambibuous_dict_test(n = 10^6)\n d = Dict()\n sizehint!(d, n)\n for i in 1:n\n d[i] = float(i)\n end\n d\nend\n\n@show typeof(ambibuous_dict_test(1))\n@benchmark ambibuous_dict_test()",
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": "typeof(ambibuous_dict_test(1)) = Dict{Any,Any}\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 117.85 MiB\n allocs estimate: 4381027\n --------------\n minimum time: 358.629 ms (0.00% GC)\n median time: 609.628 ms (41.47% GC)\n mean time: 703.607 ms (46.96% GC)\n maximum time: 1.584 s (71.75% GC)\n --------------\n samples: 8\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function Int_Float_dict_test(n = 10^6)\n S = typeof(n)\n d = Dict{S,float(S)}()\n sizehint!(d, n)\n for i in one(S):n\n d[i] = float(i)\n end\n d\nend\n\n@show typeof(Int_Float_dict_test(1))\n@benchmark Int_Float_dict_test()",
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": "typeof(Int_Float_dict_test(1)) = Dict{Int64,Float64}\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 51.00 MiB\n allocs estimate: 13\n --------------\n minimum time: 63.945 ms (0.00% GC)\n median time: 71.923 ms (6.34% GC)\n mean time: 75.880 ms (8.92% GC)\n maximum time: 131.741 ms (49.50% GC)\n --------------\n samples: 66\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@show typeof(Int_Float_dict_test(Int32(1)))\n@benchmark Int_Float_dict_test(Int32(10^6))",
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": "typeof(Int_Float_dict_test(Int32(1))) = Dict{Int32,Float64}\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 39.00 MiB\n allocs estimate: 13\n --------------\n minimum time: 58.741 ms (0.00% GC)\n median time: 64.659 ms (6.22% GC)\n mean time: 74.735 ms (8.72% GC)\n maximum time: 160.936 ms (43.47% GC)\n --------------\n samples: 67\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "struct FastHashInt{T<:Integer}; i::T; end\nBase.:(==)(x::FastHashInt, y::FastHashInt) = x.i == y.i\nBase.hash(x::FastHashInt, h::UInt) = xor(UInt(x.i), h)\nfunction Base.setindex!(d::Dict{FastHashInt{S},T}, v, i::S) where {S<:Integer,T}\n d[FastHashInt(i)] = v\nend\nBase.getindex(d::Dict{FastHashInt{S},T}, i::S) where {S<:Integer,T} = d[FastHashInt(i)]\n\nfunction FastHashInt_Float_dict_test(n = 10^6)\n S = typeof(n)\n d = Dict{FastHashInt{S},float(S)}()\n sizehint!(d, n)\n for i in one(S):n\n d[i] = float(i)\n end\n d\nend\n\n@show typeof(FastHashInt_Float_dict_test(1))\n@benchmark FastHashInt_Float_dict_test()",
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": "typeof(FastHashInt_Float_dict_test(1)) = Dict{FastHashInt{Int64},Float64}\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 51.00 MiB\n allocs estimate: 13\n --------------\n minimum time: 19.903 ms (0.00% GC)\n median time: 23.039 ms (11.92% GC)\n mean time: 26.641 ms (14.21% GC)\n maximum time: 77.939 ms (74.69% GC)\n --------------\n samples: 188\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "@show typeof(FastHashInt_Float_dict_test(Int32(1)))\n@benchmark FastHashInt_Float_dict_test(Int32(10^6))",
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": "typeof(FastHashInt_Float_dict_test(Int32(1))) = Dict{FastHashInt{Int32},Float64}\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 39.00 MiB\n allocs estimate: 13\n --------------\n minimum time: 18.376 ms (0.00% GC)\n median time: 21.169 ms (11.44% GC)\n mean time: 23.382 ms (13.92% GC)\n maximum time: 77.882 ms (77.07% GC)\n --------------\n samples: 214\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "julia-1.1",
"display_name": "Julia 1.1.1",
"language": "julia"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.1.1"
},
"@webio": {
"lastKernelId": null,
"lastCommId": null
},
"gist": {
"id": "",
"data": {
"description": "Dict test",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment