Skip to content

Instantly share code, notes, and snippets.

@JeffBezanson
Last active August 29, 2015 14: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 JeffBezanson/ea224aa45e2242ca3ee9 to your computer and use it in GitHub Desktop.
Save JeffBezanson/ea224aa45e2242ca3ee9 to your computer and use it in GitHub Desktop.
simple Dict benchmarks
function stress1(keys::Array{Int,1})
x = Dict{Int,Float64}()
for i in keys
x[i] = 0.0
end
return x
end
function stress_strings(n::Int)
d = Dict{String,Int}()
for i = 1:n
d[string(i)] = i
end
return d
end
N = 10^6;
n = [1:N];
A = mean([@elapsed stress1(n) for i=1:5])
n = shuffle([1:N]);
B = mean([@elapsed stress1(n) for i=1:5])
n = rand(0:100,N);
C = mean([@elapsed stress1(n) for i=1:5])
n = rand(0:100000000,N);
D = mean([@elapsed stress1(n) for i=1:5])
E = mean([@elapsed stress_strings(N) for i in 1:5])
x = stress1(rand(0:100_000_000,N))
F = mean([@elapsed copy(x) for i in 1:5])
d = stress_strings(N)
G = mean([@elapsed copy(d) for i in 1:5])
@printf("%10s %7s %7s %7s %7s %7s %7s %7s\n", "","A","B","C","D","E","F","G")
println("-------------------------------------------------------------------------")
@printf("%10s %6.3fs %6.3fs %6.3fs %6.3fs %6.3fs %6.3fs %6.3fs\n", "julia", A, B, C, D, E, F, G)
gc(); gc()
println()
function iterate(x)
for (k,v) in x
end
end
iterate(x)
println("iteration:")
@time for i=1:100;iterate(x);end
println()
V = rand(1:100000, 10000)
x = rand(1:100000, 1000)
setdiff(V,x)
println("setdiff:")
@time for i=1:100;setdiff(V,x);end
findin(V,x)
println("findin:")
@time for i=1:100;findin(V,x);end
#=
0.3:
A B C D E F G
-------------------------------------------------------------------------
julia 0.124s 0.119s 0.022s 0.121s 0.829s 0.117s 1.220s
iteration:
elapsed time: 1.60395415 seconds (0 bytes allocated)
setdiff:
elapsed time: 0.695045927 seconds (135756800 bytes allocated, 54.36% gc time)
findin:
elapsed time: 0.068566604 seconds (21436000 bytes allocated)
k_nucleotide 61.492 77.573 71.117 8.113
meteor_contest 3800.629 3881.834 3824.997 33.224
0.4 master:
A B C D E F G
--------------------------------------------------------------------
julia 0.134s 0.125s 0.024s 0.118s 1.061s 0.097s 1.141s
iteration:
elapsed time: 1.588071976 seconds (0 bytes allocated)
setdiff:
elapsed time: 0.45501331 seconds (155 MB allocated, 2.78% gc time in 7 pauses with 0 full sweep)
findin:
elapsed time: 0.119519514 seconds (35 MB allocated, 3.05% gc time in 2 pauses with 0 full sweep)
k_nucleotide 63.669 70.843 64.786 1.529
meteor_contest 3404.115 3481.797 3431.484 35.446
0.4 with Dict change:
A B C D E F G
-------------------------------------------------------------------------
julia 0.178s 0.163s 0.024s 0.157s 0.741s 0.168s 0.651s
iteration:
elapsed time: 0.116252711 seconds (0 bytes allocated)
setdiff:
elapsed time: 0.368042818 seconds (160 MB allocated, 1.38% gc time in 7 pauses with 0 full sweep)
findin:
elapsed time: 0.110832393 seconds (37 MB allocated, 1.40% gc time in 2 pauses with 0 full sweep)
k_nucleotide 61.301 67.434 62.914 1.601
meteor_contest 3340.690 3387.913 3365.228 19.434
=#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment