Skip to content

Instantly share code, notes, and snippets.

@remusao
Last active August 29, 2015 13:56
Show Gist options
  • Save remusao/9344557 to your computer and use it in GitHub Desktop.
Save remusao/9344557 to your computer and use it in GitHub Desktop.
Bench between Array and Dict in Julia
function findarg(args::Vector{(Symbol, ASCIIString)}, key::Symbol, default)
for (s, v) in args
is(key, s) && return v
end
return default
end
function benchDict(kwargs::Dict{Symbol, ASCIIString}, keys::Vector{Symbol})
for i = 1:100000
for s in keys
res = get(kwargs, s, "Impossible")
end
end
end
function benchArray(kwargs::Vector{(Symbol, ASCIIString)}, keys::Vector{Symbol})
for i = 1:100000
for s in keys
res = findarg(kwargs, s, "Impossible")
end
end
end
function bench(output::Bool)
for nbElem in 1:100
# Create keys
keys = [symbol(randstring(10)) for i = 1:nbElem]
# Create values
values = [randstring(10) for i = 1:nbElem]
# Dict
kwargs = Dict{Symbol, ASCIIString}(keys, values)
t1 = @elapsed benchDict(kwargs, keys)
# Array
kwargs = collect(zip(keys, values))
t2 = @elapsed benchArray(kwargs, keys)
if output
println("Test $nbElem, t_dict: $t1, t_array: $t2")
if (t1 - t2) <= 0
println("> \033[31m", "Dict", "\033[37m", " is faster: $(t2 - t1)")
else
println("> \033[32m", "Array", "\033[37m", " is faster: $(t1 - t2)")
end
end
end
end
bench(false)
bench(true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment