Skip to content

Instantly share code, notes, and snippets.

@kmsquire
Created August 13, 2013 02:09
Show Gist options
  • Save kmsquire/6217215 to your computer and use it in GitHub Desktop.
Save kmsquire/6217215 to your computer and use it in GitHub Desktop.
Performance tests for OrderedDict type (https://github.com/JuliaLang/julia/pull/4038)
# To test pre-patch Dict performance
n = 100000
srand(0x123456)
strs = [randstring(10) for i = 1:n];
nums = rand(Int, n);
# regular map
function dict_insertion_test(d::Dict)
empty!(d)
gc()
t = 0.0
for i = 1:n
t += @elapsed (d[strs[i]] = nums[i])
end
t
end
function dict_deletion_test(d::Dict, iters::Int)
dict_insertion_test(d)
gc()
t = 0.0
for i in rand(1:n, iters)
t += @elapsed delete!(d, strs[i], 0)
end
t
end
function dict_ins_del_test(d::Dict, iters::Int)
dict_insertion_test(d)
gc()
t = 0.0
for i in rand(1:n, iters)
t += randbool()? (@elapsed delete!(d, strs[i], 0)) : (@elapsed (d[strs[i]] = nums[i]))
end
t
end
function test_ins(T::Type, iter::Int)
d = T{String,Int}()
t = 0.0
for i = 1:iter
t += dict_insertion_test(d)
end
t
end
function test_ins_del(T::Type, iter::Int)
d = T{String,Int}()
t = 0.0
for i = 1:iter
t += dict_ins_del_test(d, 100000)
end
t
end
function test_del(T::Type, iter::Int)
d = T{String,Int}()
t = 0.0
for i = 1:iter
t += dict_deletion_test(d, 100000)
end
t
end
function run_all()
for test in [test_ins, test_del, test_ins_del]
println(test)
println("="^length(string(test)))
for T in [Dict] #, OrderedDict]
print("$T: ")
times = Float64[test(T, 5) for i = 1:5]
println("$times, median=$(median(times))")
end
println()
end
end
# To test new OrderedDict performance
n = 100000
srand(0x123456)
strs = [randstring(10) for i = 1:n];
nums = rand(Int, n);
# regular map
function dict_insertion_test{K,V,O}(d::Dict{K,V,O})
empty!(d)
gc()
t = 0.0
for i = 1:n
t += @elapsed (d[strs[i]] = nums[i])
end
t
end
function dict_deletion_test{K,V,O}(d::Dict{K,V,O}, iters::Int)
dict_insertion_test(d)
gc()
t = 0.0
for i in rand(1:n, iters)
t += @elapsed delete!(d, strs[i], 0)
end
t
end
function dict_ins_del_test{K,V,O}(d::Dict{K,V,O}, iters::Int)
dict_insertion_test(d)
gc()
t = 0.0
for i in rand(1:n, iters)
t += randbool()? (@elapsed delete!(d, strs[i], 0)) : (@elapsed (d[strs[i]] = nums[i]))
end
t
end
function test_ins(T::Type, iter::Int)
d = Dict{String,Int,T}()
t = 0.0
for i = 1:iter
srand(0x123456)
t += dict_insertion_test(d)
end
t
end
function test_ins_del(T::Type, iter::Int)
d = Dict{String,Int,T}()
t = 0.0
for i = 1:iter
srand(0x123456)
t += dict_ins_del_test(d, 100000)
end
t
end
function test_del(T::Type, iter::Int)
d = Dict{String,Int,T}()
t = 0.0
for i = 1:iter
srand(0x123456)
t += dict_deletion_test(d, 100000)
end
t
end
function run_all()
for test in [test_ins, test_del, test_ins_del]
println(test)
println("="^length(string(test)))
for T in [Unordered,Ordered]
print("Dict{String,Int,$T}: ")
times = Float64[test(T, 5) for i = 1:5]
println("$times, median=$(median(times))")
end
println()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment