Skip to content

Instantly share code, notes, and snippets.

@simonster
Last active August 29, 2015 13:56
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 simonster/8908354 to your computer and use it in GitHub Desktop.
Save simonster/8908354 to your computer and use it in GitHub Desktop.
Benchmarks of prototypes of different Enum options
import Base.length
immutable MyOrdinalValue12
index::Uint8
pool::Vector{ASCIIString}
end
immutable MyPooledDataArray1
index::Vector{Uint8}
pool::Vector{ASCIIString}
end
immutable MyOrdinalValue3
index::Uint8
enum::Uint32
end
immutable MyPooledDataArray3
index::Vector{Uint8}
enum::Uint32
end
function make_type(pool)
tn = gensym()
eval(quote
immutable $tn
index::Uint8
end
pool(T::$tn) = pool
$tn
end)
end
getindex(v::MyPooledDataArray1, i::Int) = MyOrdinalValue12(v.index[i], v.pool)
length(v::MyPooledDataArray1) = length(v.index)
getindex(v::MyPooledDataArray3, i::Int) = MyOrdinalValue3(v.index[i], v.enum)
length(v::MyPooledDataArray3) = length(v.index)
function test_access(x)
s = 0
for i = 1:length(x)
s += x[i].index
end
end
function test(silent::Bool)
n_data = 1_000
n_vars = 100
x = convert(Vector{Uint8}, rand(1:3, n_data))
t = @elapsed for i = 1:n_vars
pool = ["level1", "level2", "level3"]
v1 = MyPooledDataArray1(x, pool)
test_access(v1)
end
silent || println("option 1: $t")
t = @elapsed for i = 1:n_vars
pool = ["level1", "level2", "level3"]
v2 = [MyOrdinalValue12(y, pool) for y in x]
test_access(v2)
end
silent || println("option 2: $t")
t = @elapsed for i = 1:n_vars
pool = ["level1", "level2", "level3"]
v3 = [MyOrdinalValue3(y, uint16(1)) for y in x]
test_access(v3)
end
silent || println("option 3: $t")
t = @elapsed for i = 1:n_vars
pool = ["level1", "level2", "level3"]
v35 = MyPooledDataArray3(x, 1)
test_access(v35)
end
silent || println("option 3.5: $t")
t = @elapsed for i = 1:n_vars
pool = ["level1", "level2", "level3"]
tn = make_type(pool)
v4 = reinterpret(tn, x)
test_access(v4)
end
silent || println("option 4: $t")
end
test(true)
test(false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment