Last active
August 29, 2015 13:56
-
-
Save simonster/8908354 to your computer and use it in GitHub Desktop.
Benchmarks of prototypes of different Enum options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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