# Speed up Array computation

**URL:** <https://discourse.julialang.org/t/speed-up-array-computation/88875>\
**Category:** New to Julia\
**Tags:** linearalgebra, arrays, matrices\
**Created:** [October 17, 2022, 9:50pm UTC](https://discourse.julialang.org/t/speed-up-array-computation/88875 "2022-10-17T21:50:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmed](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ahmed](https://discourse.julialang.org/u/ahmed)\
**Post date:** [October 17, 2022, 9:50pm UTC](https://discourse.julialang.org/t/speed-up-array-computation/88875/1 "2022-10-17T21:50:50Z")

</div>

I have an array of matrices whose columns are used as vectors to do the following calculation in the function E\_int

```julia
nb=6; #number of columns
dim=120; #number of rows
np=400; #number of matrices in the array
function H0(n::Integer)
half=rand(ComplexF64,dim,dim);	
H0=half+adjoint(half);
	return H0
end
function E_int()
u_list=[rand(ComplexF64,dim,nb-1) for i=1:np];
En=sum(dot(u_list[n][:,i],H0(n)*u_list[n][:,i]) for n=1:length(u_list),i=1:nb-1)/(np^2);
	return En
end

```

Calculating the sum En scales badly in the computation time and the memory allocation as the dimension increases. For example that is the output of @time

```julia
0.806685 seconds (142.97 k allocations: 901.410 MiB, 9.86% gc time, 19.40% compilation time)

```

Any advice how to efficiently do this kind of computation would be appreciated.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 17, 2022, 9:51pm UTC](https://discourse.julialang.org/t/speed-up-array-computation/88875/2 "2022-10-17T21:51:41Z")

</div>

`@views`

---

<div class="post-metadata">

**Author:** ![ahmed](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ahmed](https://discourse.julialang.org/u/ahmed)\
**Post date:** [October 17, 2022, 10:00pm UTC](https://discourse.julialang.org/t/speed-up-array-computation/88875/3 "2022-10-17T22:00:09Z")

</div>

I actually did try using @views in the following way

```julia
En=@views sum(dot(u_list[n][:,i],H0(n)*u_list[n][:,i]) for n=1:length(u_list),i=1:nb-1)/(np^2)

```

It didn’t seem to improve the performance however (maybe I’m not using it correctly?)  
This was the output of time when using @views

```julia
0.821903 seconds (145.36 k allocations: 894.015 MiB, 7.92% gc time, 22.87% compilation time)

```

Thanks for your reply!

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 17, 2022, 10:29pm UTC](https://discourse.julialang.org/t/speed-up-array-computation/88875/4 "2022-10-17T22:29:41Z")

</div>

> [@ahmed](#):
>
> ```julia
> nb=6; #number of columns
> dim=120; #number of rows
> np=400; #
> 
> ```

These are all non constant global variables. See: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 17, 2022, 10:39pm UTC](https://discourse.julialang.org/t/speed-up-array-computation/88875/5 "2022-10-17T22:39:20Z")

</div>

Note also that you can use the 3-argument `dot` function here
