Last active
January 2, 2016 06:08
-
-
Save nalimilan/8261056 to your computer and use it in GitHub Desktop.
A small puzzle...
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
function test1(x::AbstractVector) | |
dims = [10] | |
a = zeros(Int, dims...) | |
for i in 1:length(x) | |
a[1] += 1 | |
end | |
a | |
end | |
function test2(x::AbstractVector) | |
dims = [10] | |
a = zeros(Int, dims...) | |
for i in 1:length(x) | |
a[1] = 1 | |
end | |
a | |
end | |
function test3(x::AbstractVector) | |
dims = (10,) | |
a = zeros(Int, dims) | |
for i in 1:length(x) | |
a[1] += 1 | |
end | |
a | |
end | |
a = [1:10000000] | |
precompile(test1, (a,)) | |
precompile(test2, (a,)) | |
precompile(test3, (a,)) | |
@time test1(a) | |
@time test2(a) | |
@time test3(a) | |
test1(a) == test3(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In 1 and 2 the dimensionality of
a
can't be inferred from the type of dims, because array length is nor part of the type.a[i] += 1
is an alias forsetindex!(a,i,getindex(a,i)+1)
, and apparently that requires a temporary variable.