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) |
vtjnash
commented
Jan 4, 2014
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 for setindex!(a,i,getindex(a,i)+1)
, and apparently that requires a temporary variable.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment