Skip to content

Instantly share code, notes, and snippets.

@genkuroki
Last active March 15, 2018 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save genkuroki/a54d45b9be99d7d81c360edabf1dbedf to your computer and use it in GitHub Desktop.
Save genkuroki/a54d45b9be99d7d81c360edabf1dbedf to your computer and use it in GitHub Desktop.
RandomIterator
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "using Distributions\nusing BenchmarkTools\n\nstruct RandomIterator <: AbstractArray{Float64,1}\n count::Int64\nend\n@inline Base.start(::RandomIterator) = 1\n@inline Base.next(R::RandomIterator, state) = (rand(), state+1)\n@inline Base.done(R::RandomIterator, state) = state > R.count\n@inline Base.eltype(::Type{RandomIterator}) = Float64\n@inline Base.length(R::RandomIterator) = R.count\n@inline Base.getindex(R::RandomIterator, i::Int) = rand()\n@inline Base.endof(R::RandomIterator) = length(R)\n@inline Base.size(R::RandomIterator) = (length(R), )\n\nfunction binomial_rv_for(n, p)\n X = Vector{Int}(n)\n for i in 1:n\n X[i] = Int(rand() < p)\n end\n X\nend\nbinomial_rv_for(10, 0.5)\n\nfunction binomial_rv_inbounds_for(n, p)\n X = Vector{Int}(n)\n @inbounds for i in 1:n\n X[i] = Int(rand() < p)\n end\n X\nend\nbinomial_rv_inbounds_for(10, 0.5)\n\nbinomial_rv_array(n, p) = Int.(rand(n) .< p)\nbinomial_rv_array(10, 0.5)\n\nbinomial_rv_Distributions(n, p) = rand(Bernoulli(p), n)\nbinomial_rv_Distributions(10, 0.5)\n\nbinomial_rv_RandomIterator(n, p) = Int.(RandomIterator(n) .< p)\nbinomial_rv_RandomIterator(10, 0.5)\n\nn, p = 10^7, 0.5\n@btime binomial_rv_for(n, p)\n@btime binomial_rv_inbounds_for(n, p)\n@btime binomial_rv_array(n, p)\n@btime binomial_rv_Distributions(n, p)\n@btime binomial_rv_RandomIterator(n, p)\n;",
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": " 49.768 ms (2 allocations: 76.29 MiB)\n 49.929 ms (2 allocations: 76.29 MiB)\n 70.532 ms (29 allocations: 152.59 MiB)\n 82.841 ms (3 allocations: 76.29 MiB)\n 48.101 ms (46 allocations: 76.30 MiB)\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gc()\n@benchmark binomial_rv_for(n, p)",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 76.29 MiB\n allocs estimate: 2\n --------------\n minimum time: 44.690 ms (0.35% GC)\n median time: 50.667 ms (11.75% GC)\n mean time: 51.363 ms (12.70% GC)\n maximum time: 110.908 ms (59.90% GC)\n --------------\n samples: 98\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gc()\n@benchmark binomial_rv_inbounds_for(n, p)",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 76.29 MiB\n allocs estimate: 2\n --------------\n minimum time: 45.690 ms (0.89% GC)\n median time: 51.288 ms (11.76% GC)\n mean time: 52.383 ms (12.82% GC)\n maximum time: 112.311 ms (59.96% GC)\n --------------\n samples: 96\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "gc()\n@benchmark binomial_rv_RandomIterator(n, p)",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "BenchmarkTools.Trial: \n memory estimate: 76.30 MiB\n allocs estimate: 46\n --------------\n minimum time: 43.460 ms (0.40% GC)\n median time: 49.451 ms (12.32% GC)\n mean time: 50.154 ms (13.40% GC)\n maximum time: 111.025 ms (60.43% GC)\n --------------\n samples: 100\n evals/sample: 1"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "julia-0.6",
"display_name": "Julia 0.6.2",
"language": "julia"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"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": "0.6.2"
},
"gist": {
"id": "a54d45b9be99d7d81c360edabf1dbedf",
"data": {
"description": "RandomIterator",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/a54d45b9be99d7d81c360edabf1dbedf"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment