Skip to content

Instantly share code, notes, and snippets.

@vchuravy
Created October 14, 2014 12:14
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 vchuravy/890a162ae3201cebf19d to your computer and use it in GitHub Desktop.
Save vchuravy/890a162ae3201cebf19d to your computer and use it in GitHub Desktop.
Performance pair vs dict
# Pair
immutable Pair{A,B}
first::A
second::B
end
start(p::Pair) = 1
done(p::Pair, i) = i>2
next(p::Pair, i) = (getfield(p,i), i+1)
indexed_next(p::Pair, i::Int, state) = (getfield(p,i), i+1)
hash(p::Pair, h::Uint) = hash(p.second, hash(p.first, h))
==(p::Pair, q::Pair) = (p.first==q.first) & (p.second==q.second)
isequal(p::Pair, q::Pair) = isequal(p.first,q.first) & isequal(p.second,q.second)
isless(p::Pair, q::Pair) = ifelse(!isequal(p.first,q.first), isless(p.first,q.first),
isless(p.second,q.second))
function getindex{T}(t::(Pair{Symbol,T}...), key::Symbol)
for pair in t
pair.first == key && return pair.second
end
end
pair1() = (Pair(:a, 1), )
pair2() = (Pair(:a, 1), Pair(:b, 2), )
pair3() = (Pair(:a, 1), Pair(:b, 2), Pair(:c, 3), )
pair4() = (Pair(:a, 1), Pair(:b, 2), Pair(:c, 3), Pair(:d, 4), )
pair5() = (Pair(:a, 1), Pair(:b, 2), Pair(:c, 3), Pair(:d, 4), Pair(:e, 5), )
pair6() = (Pair(:a, 1), Pair(:b, 2), Pair(:c, 3), Pair(:d, 4), Pair(:e, 5), Pair(:f, 6), )
pair7() = (Pair(:a, 1), Pair(:b, 2), Pair(:c, 3), Pair(:d, 4), Pair(:e, 5), Pair(:f, 6), Pair(:g, 7),)
pair8() = (Pair(:a, 1), Pair(:b, 2), Pair(:c, 3), Pair(:d, 4), Pair(:e, 5), Pair(:f, 6), Pair(:g, 7), Pair(:h, 8),)
pair9() = (Pair(:a, 1), Pair(:b, 2), Pair(:c, 3), Pair(:d, 4), Pair(:e, 5), Pair(:f, 6), Pair(:g, 7), Pair(:h, 8), Pair(:i, 9),)
pair10() = (Pair(:a, 1), Pair(:b, 2), Pair(:c, 3), Pair(:d, 4), Pair(:e, 5), Pair(:f, 6), Pair(:g, 7), Pair(:h, 8), Pair(:i, 9), Pair(:j, 10),)
dict1() = [:a => 1]
dict2() = [:a => 1, :b => 2]
dict3() = [:a => 1, :b => 2, :c => 3]
dict4() = [:a => 1, :b => 2, :c => 3, :d => 4]
dict5() = [:a => 1, :b => 2, :c => 3, :d => 4, :e => 5]
dict6() = [:a => 1, :b => 2, :c => 3, :d => 4, :e => 5, :f => 6]
dict7() = [:a => 1, :b => 2, :c => 3, :d => 4, :e => 5, :f => 6, :g => 7]
dict8() = [:a => 1, :b => 2, :c => 3, :d => 4, :e => 5, :f => 6, :g => 7, :h => 8]
dict9() = [:a => 1, :b => 2, :c => 3, :d => 4, :e => 5, :f => 6, :g => 7, :h => 8, :i => 9]
dict10() = [:a => 1, :b => 2, :c => 3, :d => 4, :e => 5, :f => 6, :g => 7, :h => 8, :i => 9, :j => 10]
const N = 10000
println("1:")
@time begin
for i in 1:N
r = pair1()
r[:a]
end
end
gc()
@time begin
for i in 1:N
r = dict1()
r[:a]
end
end
gc()
println("2:")
@time begin
for i in 1:N
r = pair2()
r[:b]
end
end
gc()
@time begin
for i in 1:N
r = dict2()
r[:b]
end
end
gc()
println("3:")
@time begin
for i in 1:N
r = pair3()
r[:c]
end
end
gc()
@time begin
for i in 1:N
r = dict3()
r[:c]
end
end
gc()
println("4:")
@time begin
for i in 1:N
r = pair4()
r[:d]
end
end
gc()
@time begin
for i in 1:N
r = dict4()
r[:d]
end
end
gc()
println("5:")
@time begin
for i in 1:N
r = pair5()
r[:e]
end
end
gc()
@time begin
for i in 1:N
r = dict5()
r[:e]
end
end
gc()
println("6:")
@time begin
for i in 1:N
r = pair6()
r[:f]
end
end
gc()
@time begin
for i in 1:N
r = dict6()
r[:f]
end
end
gc()
println("7:")
@time begin
for i in 1:N
r = pair7()
r[:g]
end
end
gc()
@time begin
for i in 1:N
r = dict7()
r[:g]
end
end
gc()
println("8:")
@time begin
for i in 1:N
r = pair8()
r[:h]
end
end
gc()
@time begin
for i in 1:N
r = dict8()
r[:h]
end
end
gc()
println("9:")
@time begin
for i in 1:N
r = pair9()
r[:i]
end
end
gc()
@time begin
for i in 1:N
r = dict9()
r[:i]
end
end
gc()
println("10:")
@time begin
for i in 1:N
r = pair10()
r[:j]
end
end
gc()
@time begin
for i in 1:N
r = dict10()
r[:j]
end
end
gc()
@vchuravy
Copy link
Author

julia test-pair.jl
1:
elapsed time: 0.000315332 seconds (480000 bytes allocated)
elapsed time: 0.00296922 seconds (5360000 bytes allocated)
2:
elapsed time: 0.000313376 seconds (800000 bytes allocated)
elapsed time: 0.003555885 seconds (5520000 bytes allocated)
3:
elapsed time: 0.000505859 seconds (1120000 bytes allocated)
elapsed time: 0.004083672 seconds (5680000 bytes allocated)
4:
elapsed time: 0.00063709 seconds (1440000 bytes allocated)
elapsed time: 0.004603988 seconds (5840000 bytes allocated)
5:
elapsed time: 0.000790111 seconds (1760000 bytes allocated)
elapsed time: 0.005275788 seconds (6000000 bytes allocated)
6:
elapsed time: 0.000876225 seconds (2080000 bytes allocated)
elapsed time: 0.006292463 seconds (6160000 bytes allocated)
7:
elapsed time: 0.001095036 seconds (2400000 bytes allocated)
elapsed time: 0.006719261 seconds (6320000 bytes allocated)
8:
elapsed time: 0.001225639 seconds (2720000 bytes allocated)
elapsed time: 0.007390852 seconds (6480000 bytes allocated)
9:
elapsed time: 0.001332357 seconds (3040000 bytes allocated)
elapsed time: 0.008455439 seconds (6640000 bytes allocated)
10:
elapsed time: 0.001571981 seconds (3360000 bytes allocated)
elapsed time: 0.009507733 seconds (6800000 bytes allocated)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment