# Minimizing Work in Copying/Manipulating Custom Data Structures

**URL:** <https://discourse.julialang.org/t/minimizing-work-in-copying-manipulating-custom-data-structures/16284>\
**Category:** Performance\
**Tags:** data\_structures\
**Created:** [October 13, 2018, 6:00pm UTC](https://discourse.julialang.org/t/minimizing-work-in-copying-manipulating-custom-data-structures/16284 "2018-10-13T18:00:58Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [October 15, 2018, 5:44pm UTC](https://discourse.julialang.org/t/minimizing-work-in-copying-manipulating-custom-data-structures/16284/11 "2018-10-15T17:44:08Z")

</div>

So the following two versions both do what I had expected. I’m originally from the MATLAB world where it would be quite natural to have the essential loop inside of scripting file that you would call. I guess the paradigm for Julia is to put everything inside of functions?

```julia
struct DataStruct{TF<:AbstractFloat, TI<:Integer}
   X::Vector{TF}
   Y::Vector{TI}
end

function struct_func!(D_work::DataStruct, D::DataStruct)
   n = length(D.X);

   for (j,k) in zip(1:n,n:-1:1)
      D_work.X[j] = sin(D.X[j] * (D.Y[k]));
      D_work.Y[j] = D.Y[j] - Int(j);
   end
   D_work, D
end

function iterate_func!(D::DataStruct,D_work::DataStruct, n)

   for j in 1:n
      D, D_work = struct_func!(D_work,D);
   end
   D, D_work
end

m = 5
S = DataStruct{Float64, Int}(range(0,stop=1, length=m), 2*ones(m));
S_work = deepcopy(S);
T = deepcopy(S);
T_work = deepcopy(S);

N = 4;

for j in 1:N
   global S, S_work = struct_func!(S_work,S);
end
iterate_func!(T, T_work, N);

```

---

_[View the full topic](https://discourse.julialang.org/t/minimizing-work-in-copying-manipulating-custom-data-structures/16284)._
