Skip to content

Instantly share code, notes, and snippets.

@mlhetland
Last active August 29, 2015 14:02
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 mlhetland/47bb6a624af3432118bb to your computer and use it in GitHub Desktop.
Save mlhetland/47bb6a624af3432118bb to your computer and use it in GitHub Desktop.
Growing a shrunken Array
using Benchmark
const n = 100000
function push_int_any()
a = Array(Any, n)
resize!(a, 0)
for i = 1:n
push!(a, i)
end
end
function asgn_int_any()
a = Array(Any, n)
for i = 1:n
a[i] = i
end
end
function asgn_check_int_any()
a = Array(Any, n)
for i = 1:n
if i < length(a)
a[i] = i
end
end
end
function asgn_check_id_uint()
a = Array(Uint, n)
for i = 1:n
if i < length(a)
a[i] = object_id(i)
end
end
end
function push_id_uint()
a = Array(Uint, n)
resize!(a, 0)
for i = 1:n
push!(a, object_id(i))
end
end
function push_int_uint()
a = Array(Uint, n)
resize!(a, 0)
for i = 1:n
push!(a, i)
end
end
type Entry
val
end
function push_entry_any()
a = Array(Any, n)
x = Entry(1)
resize!(a, 0)
for i = 1:n
push!(a, x)
end
end
function push_enryid_uint()
a = Array(Uint, n)
x = Entry(1)
id = object_id(x)
resize!(a, 0)
for i = 1:n
push!(a, id)
end
end
function push_int_int()
a = Array(Int, n)
resize!(a, 0)
for i = 1:n
push!(a, i)
end
end
function push_uint_uint()
a = Array(Uint, n)
resize!(a, 0)
for i = uint(1):uint(n)
push!(a, i)
end
end
@show compare([
push_int_any
asgn_int_any
asgn_check_int_any
asgn_check_id_uint
push_id_uint
push_int_uint
push_entry_any
push_enryid_uint
push_int_int
push_uint_uint
], 100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment