{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO: Recompiling stale cache file /home/dzea/.julia/lib/v0.5/PairwiseListMatrices.ji for module PairwiseListMatrices.\n",
"WARNING: Method definition require(Symbol) in module Base at loading.jl:345 overwritten in module Main at /home/dzea/.julia/v0.5/Requires/src/require.jl:12.\n"
]
}
],
"source": [
"using PairwiseListMatrices\n",
"using BenchmarkTools\n",
"using Base.Test\n",
"using Gadfly"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"using_pairwiselistmatrix (generic function with 1 method)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Similar to pairwise of Distances.jl\n",
"function using_full(vecs)\n",
" n = length(vecs)\n",
" mat = Array(Float64, n, n)\n",
" @inbounds for i in 1:n\n",
" vec_i = vecs[i]\n",
" for j in i:n\n",
" mat[i,j] = cor(vec_i, vecs[j])\n",
" end\n",
" for j = 1 : (i-1)\n",
" @inbounds mat[i,j] = mat[j,i] # leveraging the symmetry\n",
" end\n",
" end\n",
" mat\n",
"end\n",
"\n",
"function using_symmetric(vecs)\n",
" n = length(vecs)\n",
" mat = Array(Float64, n, n)\n",
" @inbounds for i in 1:n\n",
" vec_i = vecs[i]\n",
" for j in i:n\n",
" mat[i,j] = cor(vec_i, vecs[j])\n",
" end\n",
" end\n",
" Symmetric(mat)\n",
"end\n",
"\n",
"function using_sparse_symmetric(vecs)\n",
" n = length(vecs)\n",
" mat = spzeros(Float64, n, n)\n",
" @inbounds for i in 1:n\n",
" vec_i = vecs[i]\n",
" for j in i:n\n",
" mat[i,j] = cor(vec_i, vecs[j])\n",
" end\n",
" end\n",
" Symmetric(mat)\n",
"end\n",
"\n",
"# Creates a list and returns it as a PairwiseListMatrix\n",
"function using_pairwiselist(vecs)\n",
" n = length(vecs)\n",
" list = Array(Float64, div(n*(n+1),2))\n",
" k = 1\n",
" @inbounds for i in 1:n\n",
" vec_i = vecs[i]\n",
" for j in i:n\n",
" list[k] = cor(vec_i, vecs[j])\n",
" k += 1\n",
" end\n",
" end\n",
" PairwiseListMatrix(list, true)\n",
"end\n",
"\n",
"# Creates and fill a PairwiseListMatrix\n",
"function using_pairwiselistmatrix(vecs)\n",
" n = length(vecs)\n",
" list = PairwiseListMatrix(Float64, n, true)\n",
" @inbounds for i in 1:n\n",
" vec_i = vecs[i]\n",
" for j in i:n\n",
" list[i,j] = cor(vec_i, vecs[j])\n",
" end\n",
" end\n",
" list\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 13.37 mb\n",
" allocs estimate: 751503\n",
" --------------\n",
" minimum time: 18.532 ms (0.00% GC)\n",
" median time: 19.266 ms (3.74% GC)\n",
" mean time: 19.439 ms (2.53% GC)\n",
" maximum time: 29.663 ms (4.11% GC)\n",
" --------------\n",
" samples: 258\n",
" evals/sample: 1\n",
" time tolerance: 5.00%\n",
" memory tolerance: 1.00%"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"const testset = [ rand(3) for n in 1:500 ];\n",
"\n",
"# Test for the same result\n",
"@test all(using_sparse_symmetric(testset) .== using_full(testset))\n",
"@test all(using_full(testset) .== using_symmetric(testset))\n",
"@test all(using_symmetric(testset) .== using_pairwiselist(testset))\n",
"@test all(using_pairwiselist(testset) .== using_pairwiselistmatrix(testset))\n",
"\n",
"@benchmark using_symmetric(testset)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 13.37 mb\n",
" allocs estimate: 751502\n",
" --------------\n",
" minimum time: 18.696 ms (0.00% GC)\n",
" median time: 19.622 ms (3.75% GC)\n",
" mean time: 20.087 ms (2.44% GC)\n",
" maximum time: 38.913 ms (2.49% GC)\n",
" --------------\n",
" samples: 250\n",
" evals/sample: 1\n",
" time tolerance: 5.00%\n",
" memory tolerance: 1.00%"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@benchmark using_full(testset)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 12.42 mb\n",
" allocs estimate: 751504\n",
" --------------\n",
" minimum time: 18.347 ms (0.00% GC)\n",
" median time: 19.309 ms (3.85% GC)\n",
" mean time: 19.000 ms (2.24% GC)\n",
" maximum time: 20.942 ms (3.55% GC)\n",
" --------------\n",
" samples: 264\n",
" evals/sample: 1\n",
" time tolerance: 5.00%\n",
" memory tolerance: 1.00%"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@benchmark using_pairwiselist(testset)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"BenchmarkTools.Trial: \n",
" memory estimate: 10.51 mb\n",
" allocs estimate: 626254\n",
" --------------\n",
" minimum time: 16.304 ms (0.00% GC)\n",
" median time: 17.048 ms (0.00% GC)\n",
" mean time: 16.932 ms (2.16% GC)\n",
" maximum time: 23.361 ms (0.00% GC)\n",
" --------------\n",
" samples: 296\n",
" evals/sample: 1\n",
" time tolerance: 5.00%\n",
" memory tolerance: 1.00%"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@benchmark using_pairwiselistmatrix(testset)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"const SAMPLES = collect(10:100:1000)\n",
"const TIME = zeros(Float64, length(SAMPLES)*5)\n",
"const NAMES = vcat([ [\"full\", \"symmetric\", \"sparse\", \"list\", \"pairwiselistmatrix\"] for i in 1:length(SAMPLES) ]...)\n",
"const XS = vcat([ [x, x, x, x, x] for x in SAMPLES ]...)\n",
"\n",
"k = 0\n",
"for sample in SAMPLES\n",
" test = [ rand(3) for n in 1:sample ]\n",
" k += 1\n",
" TIME[k] = @elapsed using_full(test)\n",
" k += 1\n",
" TIME[k] = @elapsed using_symmetric(test)\n",
" k += 1\n",
" TIME[k] = sample <= 410 ? @elapsed( using_sparse_symmetric(test) ) : NaN\n",
" k += 1\n",
" TIME[k] = @elapsed using_pairwiselist(test)\n",
" k += 1\n",
" TIME[k] = @elapsed using_pairwiselistmatrix(test)\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
],
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Plot(...)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"plot(x=XS, y=TIME, color=NAMES, Geom.point, Scale.y_sqrt)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
],
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Plot(...)"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"plot(x=XS, y=TIME, color=NAMES, Geom.smooth)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 0.5.0",
"language": "julia",
"name": "julia-0.5"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "0.5.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}