In [45]:
function version1(N)
    b = [1.0;zeros(N-1)]
  println(typeof(b))
  for k = 1:N
    for j = 1:N
      b[j] += k
    end
  end
  b
end


WARNING: Method definition version1(Any) in module Main at In[35]:2 overwritten at In[45]:2.
Out[45]:
version1 (generic function with 1 method)

In [46]:
@code_warntype(version1(10))


Variables:
  #self#::#version1
  N::Int64
  b::ARRAY{T,N}
  #temp#@_4::Int64
  k::Int64
  #temp#@_6::Int64
  j::Int64

Body:
  begin  # In[45], line 2:
      GenSym(7) = (Base.fill!)((top(ccall))(:jl_alloc_array_1d,(top(apply_type))(Core.Array,Float64,1)::Type{Array{Float64,1}},(top(svec))(Core.Any,Core.Int)::SimpleVector,Array{Float64,1},0,(Base.box)(Int64,(Base.sub_int)(N::Int64,1)),0)::Array{Float64,1},(Base.box)(Float64,(Base.sitofp)(Float64,0)))::Array{Float64,1} # abstractarray.jl, line 751:
      b::ARRAY{T,N} = (Base.cat)(1,1.0,GenSym(7))::ARRAY{T,N} # In[45], line 3:
      (Base.println)(Base.STDOUT,(Main.typeof)(b::ARRAY{T,N})::TYPE{_<:ARRAY{T,N}})::ANY # In[45], line 4: # range.jl, line 83: # range.jl, line 70:
      (top(getfield))(Base,:UnitRange)::Type{UnitRange{T<:Real}}
      GenSym(8) = (Base.select_value)((Base.sle_int)(1,N::Int64)::Bool,N::Int64,(Base.box)(Int64,(Base.sub_int)(1,1)))::Int64
      #temp#@_4::Int64 = 1
      13: 
      unless (Base.box)(Base.Bool,(Base.not_int)((#temp#@_4::Int64 === (Base.box)(Int64,(Base.add_int)(GenSym(8),1)))::Bool)) goto 40
      GenSym(9) = #temp#@_4::Int64
      GenSym(10) = (Base.box)(Int64,(Base.add_int)(#temp#@_4::Int64,1))
      k::Int64 = GenSym(9)
      #temp#@_4::Int64 = GenSym(10) # In[45], line 5: # range.jl, line 83: # range.jl, line 70:
      (top(getfield))(Base,:UnitRange)::Type{UnitRange{T<:Real}}
      GenSym(11) = (Base.select_value)((Base.sle_int)(1,N::Int64)::Bool,N::Int64,(Base.box)(Int64,(Base.sub_int)(1,1)))::Int64
      #temp#@_6::Int64 = 1
      25: 
      unless (Base.box)(Base.Bool,(Base.not_int)((#temp#@_6::Int64 === (Base.box)(Int64,(Base.add_int)(GenSym(11),1)))::Bool)) goto 36
      GenSym(12) = #temp#@_6::Int64
      GenSym(13) = (Base.box)(Int64,(Base.add_int)(#temp#@_6::Int64,1))
      j::Int64 = GenSym(12)
      #temp#@_6::Int64 = GenSym(13) # In[45], line 6:
      GenSym(4) = ((Base.arrayref)(b::ARRAY{T,N},j::Int64)::ANY + k::Int64)::ANY
      (Main.setindex!)(b::ARRAY{T,N},GenSym(4),j::Int64)::ARRAY{T,N}
      34: 
      goto 25
      36: 
      37: 
      38: 
      goto 13
      40: 
      41:  # In[45], line 9:
      return b::ARRAY{T,N}
  end::ARRAY{T,N}

In [47]:
function version2(N)
  b = zeros(N)
  b[1] = 1
  println(typeof(b))
  for k = 1:N
    for j = 1:N
      b[j] += k
    end
  end
  b
end


WARNING: Method definition version2(Any) in module Main at In[37]:2 overwritten at In[47]:2.
Out[47]:
version2 (generic function with 1 method)

In [48]:
@code_warntype(version2(10))


Variables:
  #self#::#version2
  N::Int64
  b::Array{Float64,1}
  #temp#@_4::Int64
  k::Int64
  #temp#@_6::Int64
  j::Int64

Body:
  begin  # In[47], line 2:
      b::Array{Float64,1} = (Base.fill!)((top(ccall))(:jl_alloc_array_1d,(top(apply_type))(Core.Array,Float64,1)::Type{Array{Float64,1}},(top(svec))(Core.Any,Core.Int)::SimpleVector,Array{Float64,1},0,N::Int64,0)::Array{Float64,1},(Base.box)(Float64,(Base.sitofp)(Float64,0)))::Array{Float64,1} # In[47], line 3:
      (Base.arrayset)(b::Array{Float64,1},(Base.box)(Float64,(Base.sitofp)(Float64,1)),1)::Array{Float64,1} # In[47], line 4:
      (Base.println)(Base.STDOUT,Array{Float64,1})::ANY # In[47], line 5: # range.jl, line 83: # range.jl, line 70:
      (top(getfield))(Base,:UnitRange)::Type{UnitRange{T<:Real}}
      GenSym(7) = (Base.select_value)((Base.sle_int)(1,N::Int64)::Bool,N::Int64,(Base.box)(Int64,(Base.sub_int)(1,1)))::Int64
      #temp#@_4::Int64 = 1
      13: 
      unless (Base.box)(Base.Bool,(Base.not_int)((#temp#@_4::Int64 === (Base.box)(Int64,(Base.add_int)(GenSym(7),1)))::Bool)) goto 42
      GenSym(8) = #temp#@_4::Int64
      GenSym(9) = (Base.box)(Int64,(Base.add_int)(#temp#@_4::Int64,1))
      k::Int64 = GenSym(8)
      #temp#@_4::Int64 = GenSym(9) # In[47], line 6: # range.jl, line 83: # range.jl, line 70:
      (top(getfield))(Base,:UnitRange)::Type{UnitRange{T<:Real}}
      GenSym(10) = (Base.select_value)((Base.sle_int)(1,N::Int64)::Bool,N::Int64,(Base.box)(Int64,(Base.sub_int)(1,1)))::Int64
      #temp#@_6::Int64 = 1
      25: 
      unless (Base.box)(Base.Bool,(Base.not_int)((#temp#@_6::Int64 === (Base.box)(Int64,(Base.add_int)(GenSym(10),1)))::Bool)) goto 38
      GenSym(11) = #temp#@_6::Int64
      GenSym(12) = (Base.box)(Int64,(Base.add_int)(#temp#@_6::Int64,1))
      j::Int64 = GenSym(11)
      #temp#@_6::Int64 = GenSym(12) # In[47], line 7:
      GenSym(6) = (Base.arrayref)(b::Array{Float64,1},j::Int64)::Float64 # promotion.jl, line 172:
      GenSym(4) = (Base.box)(Base.Float64,(Base.add_float)(GenSym(6),(Base.box)(Float64,(Base.sitofp)(Float64,k::Int64))))
      (Base.arrayset)(b::Array{Float64,1},GenSym(4),j::Int64)::Array{Float64,1}
      36: 
      goto 25
      38: 
      39: 
      40: 
      goto 13
      42: 
      43:  # In[47], line 10:
      return b::Array{Float64,1}
  end::Array{Float64,1}

In [58]:
function version3(N)
    b = Array{Float64,1}[1.0;zeros(N-1)]
  println(typeof(b))
  for k = 1:N
    for j = 1:N
      b[j] += k
    end
  end
  b
end


WARNING: Method definition version3(Any) in module Main at In[49]:2 overwritten at In[58]:2.
Out[58]:
version3 (generic function with 1 method)

In [59]:
@code_warntype(version3(10))


Variables:
  #self#::#version3
  N::Int64
  b::ARRAY{T,N}
  #temp#@_4::Int64
  k::Int64
  #temp#@_6::Int64
  j::Int64

Body:
  begin  # In[58], line 2:
      GenSym(7) = (Base.fill!)((top(ccall))(:jl_alloc_array_1d,(top(apply_type))(Core.Array,Float64,1)::Type{Array{Float64,1}},(top(svec))(Core.Any,Core.Int)::SimpleVector,Array{Float64,1},0,(Base.box)(Int64,(Base.sub_int)(N::Int64,1)),0)::Array{Float64,1},(Base.box)(Float64,(Base.sitofp)(Float64,0)))::Array{Float64,1} # abstractarray.jl, line 754:
      b::ARRAY{T,N} = (Base.cat_t)(1,Array{Float64,1},1.0,GenSym(7))::ARRAY{T,N} # In[58], line 3:
      (Base.println)(Base.STDOUT,(Main.typeof)(b::ARRAY{T,N})::TYPE{_<:ARRAY{T,N}})::ANY # In[58], line 4: # range.jl, line 83: # range.jl, line 70:
      (top(getfield))(Base,:UnitRange)::Type{UnitRange{T<:Real}}
      GenSym(8) = (Base.select_value)((Base.sle_int)(1,N::Int64)::Bool,N::Int64,(Base.box)(Int64,(Base.sub_int)(1,1)))::Int64
      #temp#@_4::Int64 = 1
      13: 
      unless (Base.box)(Base.Bool,(Base.not_int)((#temp#@_4::Int64 === (Base.box)(Int64,(Base.add_int)(GenSym(8),1)))::Bool)) goto 40
      GenSym(9) = #temp#@_4::Int64
      GenSym(10) = (Base.box)(Int64,(Base.add_int)(#temp#@_4::Int64,1))
      k::Int64 = GenSym(9)
      #temp#@_4::Int64 = GenSym(10) # In[58], line 5: # range.jl, line 83: # range.jl, line 70:
      (top(getfield))(Base,:UnitRange)::Type{UnitRange{T<:Real}}
      GenSym(11) = (Base.select_value)((Base.sle_int)(1,N::Int64)::Bool,N::Int64,(Base.box)(Int64,(Base.sub_int)(1,1)))::Int64
      #temp#@_6::Int64 = 1
      25: 
      unless (Base.box)(Base.Bool,(Base.not_int)((#temp#@_6::Int64 === (Base.box)(Int64,(Base.add_int)(GenSym(11),1)))::Bool)) goto 36
      GenSym(12) = #temp#@_6::Int64
      GenSym(13) = (Base.box)(Int64,(Base.add_int)(#temp#@_6::Int64,1))
      j::Int64 = GenSym(12)
      #temp#@_6::Int64 = GenSym(13) # In[58], line 6:
      GenSym(4) = ((Base.arrayref)(b::ARRAY{T,N},j::Int64)::ANY + k::Int64)::ANY
      (Main.setindex!)(b::ARRAY{T,N},GenSym(4),j::Int64)::ARRAY{T,N}
      34: 
      goto 25
      36: 
      37: 
      38: 
      goto 13
      40: 
      41:  # In[58], line 9:
      return b::ARRAY{T,N}
  end::ARRAY{T,N}

In [51]:
function version4(N)
    b = [1.0;zeros(N-1)]::Vector{Float64}
  println(typeof(b))
  for k = 1:N
    for j = 1:N
      b[j] += k
    end
  end
  b
end


Out[51]:
version4 (generic function with 1 method)

In [52]:
@code_warntype(version4(10))


Variables:
  #self#::#version4
  N::Int64
  b::Array{Float64,1}
  #temp#@_4::Int64
  k::Int64
  #temp#@_6::Int64
  j::Int64

Body:
  begin  # In[51], line 2:
      GenSym(8) = (Base.fill!)((top(ccall))(:jl_alloc_array_1d,(top(apply_type))(Core.Array,Float64,1)::Type{Array{Float64,1}},(top(svec))(Core.Any,Core.Int)::SimpleVector,Array{Float64,1},0,(Base.box)(Int64,(Base.sub_int)(N::Int64,1)),0)::Array{Float64,1},(Base.box)(Float64,(Base.sitofp)(Float64,0)))::Array{Float64,1} # abstractarray.jl, line 751:
      b::Array{Float64,1} = (top(typeassert))((Base.cat)(1,1.0,GenSym(8))::ARRAY{T,N},Array{Float64,1})::Array{Float64,1} # In[51], line 3:
      (Base.println)(Base.STDOUT,Array{Float64,1})::ANY # In[51], line 4: # range.jl, line 83: # range.jl, line 70:
      (top(getfield))(Base,:UnitRange)::Type{UnitRange{T<:Real}}
      GenSym(9) = (Base.select_value)((Base.sle_int)(1,N::Int64)::Bool,N::Int64,(Base.box)(Int64,(Base.sub_int)(1,1)))::Int64
      #temp#@_4::Int64 = 1
      13: 
      unless (Base.box)(Base.Bool,(Base.not_int)((#temp#@_4::Int64 === (Base.box)(Int64,(Base.add_int)(GenSym(9),1)))::Bool)) goto 42
      GenSym(10) = #temp#@_4::Int64
      GenSym(11) = (Base.box)(Int64,(Base.add_int)(#temp#@_4::Int64,1))
      k::Int64 = GenSym(10)
      #temp#@_4::Int64 = GenSym(11) # In[51], line 5: # range.jl, line 83: # range.jl, line 70:
      (top(getfield))(Base,:UnitRange)::Type{UnitRange{T<:Real}}
      GenSym(12) = (Base.select_value)((Base.sle_int)(1,N::Int64)::Bool,N::Int64,(Base.box)(Int64,(Base.sub_int)(1,1)))::Int64
      #temp#@_6::Int64 = 1
      25: 
      unless (Base.box)(Base.Bool,(Base.not_int)((#temp#@_6::Int64 === (Base.box)(Int64,(Base.add_int)(GenSym(12),1)))::Bool)) goto 38
      GenSym(13) = #temp#@_6::Int64
      GenSym(14) = (Base.box)(Int64,(Base.add_int)(#temp#@_6::Int64,1))
      j::Int64 = GenSym(13)
      #temp#@_6::Int64 = GenSym(14) # In[51], line 6:
      GenSym(7) = (Base.arrayref)(b::Array{Float64,1},j::Int64)::Float64 # promotion.jl, line 172:
      GenSym(4) = (Base.box)(Base.Float64,(Base.add_float)(GenSym(7),(Base.box)(Float64,(Base.sitofp)(Float64,k::Int64))))
      (Base.arrayset)(b::Array{Float64,1},GenSym(4),j::Int64)::Array{Float64,1}
      36: 
      goto 25
      38: 
      39: 
      40: 
      goto 13
      42: 
      43:  # In[51], line 9:
      return b::Array{Float64,1}
  end::Array{Float64,1}

In [53]:
N = 1000
println("compiling..")
version1(N);
version2(N);
version3(N);


compiling..
Array{Float64,1}
Array{Float64,1}
LoadError: MethodError: Cannot `convert` an object of type Float64 to an object of type Array{Float64,1}
This may have arisen from a call to the constructor Array{Float64,1}(...),
since type constructors fall back to convert methods.
Closest candidates are:
  convert{T}(::Type{Array{T,1}}, !Matched::Range{T})
  convert{T,n}(::Type{Array{T,n}}, !Matched::Array{T,n})
  convert{T,S,N}(::Type{Array{T,N}}, !Matched::Base.ReshapedArray{S,N,P<:AbstractArray{T,N},MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64}}}})
  ...
while loading In[53], in expression starting on line 5

In [54]:
println("Version 1")
@time version1(N)
res = version1(5)
(typeof(res), size(res))


Version 1
Array{Float64,1}
  0.115584 seconds (3.47 M allocations: 52.923 MB, 11.72% gc time)
Array{Float64,1}
Out[54]:
(Array{Float64,1},(5,))

In [55]:
println("Version 2")
@time version2(N)
res = version2(5)
(typeof(res), size(res))


Version 2
Array{Float64,1}
  0.002064 seconds (191 allocations: 11.859 KB)
Array{Float64,1}
Out[55]:
(Array{Float64,1},(5,))

In [56]:
println("Version 3")
@time version3(N)
res = version3(5)
(typeof(res), size(res))


Version 3
LoadError: MethodError: Cannot `convert` an object of type Float64 to an object of type Array{Float64,1}
This may have arisen from a call to the constructor Array{Float64,1}(...),
since type constructors fall back to convert methods.
Closest candidates are:
  convert{T}(::Type{Array{T,1}}, !Matched::Range{T})
  convert{T,n}(::Type{Array{T,n}}, !Matched::Array{T,n})
  convert{T,S,N}(::Type{Array{T,N}}, !Matched::Base.ReshapedArray{S,N,P<:AbstractArray{T,N},MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64}}}})
  ...
while loading In[56], in expression starting on line 155

In [57]:
println("Version 4")
@time version3(N)
res = version3(5)
(typeof(res), size(res))


Version 4
LoadError: MethodError: Cannot `convert` an object of type Float64 to an object of type Array{Float64,1}
This may have arisen from a call to the constructor Array{Float64,1}(...),
since type constructors fall back to convert methods.
Closest candidates are:
  convert{T}(::Type{Array{T,1}}, !Matched::Range{T})
  convert{T,n}(::Type{Array{T,n}}, !Matched::Array{T,n})
  convert{T,S,N}(::Type{Array{T,N}}, !Matched::Base.ReshapedArray{S,N,P<:AbstractArray{T,N},MI<:Tuple{Vararg{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64}}}})
  ...
while loading In[57], in expression starting on line 155