Hello all,

The following minimal example is confusing me. In each case the same 
operations are performed, in several cases it seems that a copy of the 
array seems to be made even though the original array is the one getting 
modified.

Any assistance as to what I am doing incorrectly would be appreciated (my 
preference would be to do something like scale2! and not have to use 
scale4! as a work around).

let
  function scale1!(f, a)
    for n = 1:length(f)
      f[n] = f[n] * a
    end
  end

  function scale2!(V, a)
    f = V["f"]
    for n = 1:length(f)
      f[n] = f[n] * a
    end
  end

  function scale3!(V, a)
    for n = 1:length(V["f"])
      V["f"][n] = V["f"][n] * a
    end
  end

  function scale4!(f, a)
    f = V["f"]
    scale1!(f,a)
  end

  f = ones(4,600000)
  a = 2.

  # Typed dictionary
  V = Dict{String,Array{Float64,2}}()
  V["f"] = f

  # untyped dictionary
  U = Dict()
  U["f"] = f

  println("pass array")
  @time scale1!(f,a)
  println()

  println("Get array from typed dictionary")
  @time scale2!(V,a)
  println()

  println("Get array from untyped dictionary")
  @time scale2!(U,a)
  println()

  println("work with typed dictionary")
  @time scale3!(V,a)
  println()

  println("work with untyped dictionary")
  @time scale3!(U,a)
  println()

  println("get array from typed dict and call scale1!")
  @time scale4!(V,a)
  println()

  println("get array from untyped dict and call scale1!")
  @time scale4!(U,a)
end


'The output I get for Julia 0.3.10 is

pass array
elapsed time: 0.00631541 seconds (108104 bytes allocated)

Get array from typed dictionary
elapsed time: 0.769137049 seconds (230636428 bytes allocated, 13.57% gc 
time)

Get array from untyped dictionary
elapsed time: 0.689766861 seconds (230487160 bytes allocated, 16.58% gc 
time)

work with typed dictionary
elapsed time: 0.218875407 seconds (167668 bytes allocated)

work with untyped dictionary
elapsed time: 1.057047219 seconds (268889924 bytes allocated, 13.12% gc 
time)

get array from typed dict and call scale1!
elapsed time: 0.006385067 seconds (27196 bytes allocated)

get array from untyped dict and call scale1!
elapsed time: 0.005792765 seconds (26724 bytes allocated)


The output I get for Julia 0.4.0-dev is

pass array
   6.036 milliseconds (2148 allocations: 104 KB)

Get array from typed dictionary
 565.849 milliseconds (12000 k allocations: 220 MB, 3.33% gc time)

Get array from untyped dictionary
 569.234 milliseconds (12000 k allocations: 220 MB, 3.17% gc time)

work with typed dictionary
 255.653 milliseconds (3513 allocations: 161 KB)

work with untyped dictionary
 836.564 milliseconds (14401 k allocations: 256 MB, 2.30% gc time)

get array from typed dict and call scale1!
   5.984 milliseconds (544 allocations: 28558 bytes)

get array from untyped dict and call scale1!
   5.180 milliseconds (539 allocations: 28014 bytes)

Reply via email to