# How to optimize this code?

**URL:** <https://discourse.julialang.org/t/how-to-optimize-this-code/74978>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [January 21, 2022, 12:07pm UTC](https://discourse.julialang.org/t/how-to-optimize-this-code/74978 "2022-01-21T12:07:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HANHAOHAN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hanhaohan/32/36366_2.png) [@HANHAOHAN](https://discourse.julialang.org/u/HANHAOHAN)\
**Post date:** [January 21, 2022, 12:07pm UTC](https://discourse.julialang.org/t/how-to-optimize-this-code/74978/1 "2022-01-21T12:07:57Z")

</div>

```nohighlight
# REPL
using VideoIO
using Images
using LSHFunctions
frms = Vector{UInt64}(undef, 25000);
hashfn = LSHFunction(cossim, 64);
function one_pixes(x::Matrix{RGB{Float32}})::Bool
               pix = x[1,1]
               for x_el in x
                       if x_el != pix return false; end
               end
               return true
       end
function frmkeyb(hfn::SimHash{Float32}, im::PermutedDimsArray{RGB{N0f8}, 2, (2, 1), (2, 1), Matrix{RGB{N0f8}}})::UInt64
               # @show typeof(im)
               h,w = size(im)
               im = im[h÷8:h*7÷8, w÷8:w*7÷8]
               h, w = size(im)
               h ÷= 20
               w ÷= 20
               resized = [RGB{Float32}(sum(im[i:i+h-1, j:j+w-1])/(h*w))
                       for (i,j) in Iterators.product(1:h:size(im)[1]-h+1, 1:w:size(im)[2]-w+1)]
               one_pixes(resized) ? zero(UInt64) : reduce((x, y)->(UInt64(x) <<1 ) | UInt64(y), hfn(reshape(rawview(channelview(resized)), :)))
       end
res(hashfn, video_path::String) = VideoIO.openvideo(video_path, thread_count=1) do vr
                                       img = read(vr);
                                       
                                       frms[1] = frmkeyb(hashfn, img)
                                       cnt = 1
                                       while !eof(vr)
                                               cnt += 1
                                               read!(vr, img);
                                               frms[cnt] = frmkeyb(hashfn, img);
                                       end
                                       (0x0032, copy(frms[1:cnt-25]))
                               end;
@time res(hashfn, "https://adxvideo.dataeye.com/6961/1f7a48d541dd71c5025f119a9fe7ddd9.mp4");

```

Now, In my pc, it output as follows  
5.338815 seconds (3.15 M allocations: 2.664 GiB, 1.62% gc time, 0.84% compilation time)

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [January 21, 2022, 12:16pm UTC](https://discourse.julialang.org/t/how-to-optimize-this-code/74978/2 "2022-01-21T12:16:56Z")

</div>

> [@HANHAOHAN](#):
>
> `im[h÷8:h*7÷8, w÷8:w*7÷8]`  
> `im[i:i+h-1, j:j+w-1]`

you may take views on your array slices (otherwise it creates a new array)

---

<div class="post-metadata">

**Author:** ![HANHAOHAN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hanhaohan/32/36366_2.png) [@HANHAOHAN](https://discourse.julialang.org/u/HANHAOHAN)\
**Post date:** [January 22, 2022, 1:41pm UTC](https://discourse.julialang.org/t/how-to-optimize-this-code/74978/3 "2022-01-22T13:41:26Z")

</div>

With view function, as following

```nohighlight
function frmkeyb(hfn::SimHash{Float32}, im::PermutedDimsArray{RGB{N0f8}, 2, (2, 1), (2, 1), Matrix{RGB{N0f8}}})::UInt64
                      # @show typeof(im)
                      h,w = size(im)
                      im = view(im, h÷8:h*7÷8, w÷8:w*7÷8)
                      h, w = size(im)
                      h ÷= 20
                      w ÷= 20
                      resized = [RGB{Float32}(sum(view(im, i:i+h-1, j:j+w-1))/(h*w))
                              for (i,j) in Iterators.product(1:h:size(im)[1]-h+1, 1:w:size(im)[2]-w+1)]
                      one_pixes(resized) ? zero(UInt64) : reduce((x, y)->(UInt64(x) <<1 ) | UInt64(y), hfn(reshape(rawview(channelview(resized)), :)))
              end

```

output is:  
5.123041 seconds (3.46 M allocations: 120.359 MiB, 0.42% gc time, 0.50% compilation time)

Thank you!

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [January 22, 2022, 1:44pm UTC](https://discourse.julialang.org/t/how-to-optimize-this-code/74978/4 "2022-01-22T13:44:33Z")

</div>

```julia
5.123041 seconds (3.46 M allocations: 120.359 MiB, 0.42% gc time, 0.50% compilation time)

```

Maybe you are measuring download time?

---

<div class="post-metadata">

**Author:** ![HANHAOHAN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hanhaohan/32/36366_2.png) [@HANHAOHAN](https://discourse.julialang.org/u/HANHAOHAN)\
**Post date:** [January 23, 2022, 10:30am UTC](https://discourse.julialang.org/t/how-to-optimize-this-code/74978/5 "2022-01-23T10:30:29Z")

</div>

Yes, download time is not important.  
Actually, the video is a local file in my project.

---

<div class="post-metadata">

**Author:** ![HANHAOHAN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hanhaohan/32/36366_2.png) [@HANHAOHAN](https://discourse.julialang.org/u/HANHAOHAN)\
**Post date:** [January 23, 2022, 10:49am UTC](https://discourse.julialang.org/t/how-to-optimize-this-code/74978/6 "2022-01-23T10:49:23Z")

</div>

@code\_warntype res(hashfn, “[https://adxvideo.dataeye.com/6961/1f7a48d541dd71c5025f119a9fe7ddd9.mp4](https://adxvideo.dataeye.com/6961/1f7a48d541dd71c5025f119a9fe7ddd9.mp4)”);  
MethodInstance for res(::SimHash{Float32}, ::String)  
from res(hashfn, video\_path::String) in Main at REPL[33]:1  
Arguments  
#self#::Core.Const(res)  
hashfn::SimHash{Float32}  
video\_path::String  
Locals  
#13::var"#13#14"{SimHash{Float32}}  
Body::Tuple{UInt16, Any}  
1 ─ %1 = Main.:(var"#13#14")::Core.Const(var"#13#14")  
│ %2 = Core.typeof(hashfn)::Core.Const(SimHash{Float32})  
│ %3 = Core.apply\_type(%1, %2)::Core.Const(var"#13#14"{SimHash{Float32}})  
│ (#13 = %new(%3, hashfn))  
│ %5 = #13::var"#13#14"{SimHash{Float32}}  
│ %6 = (:thread\_count,)::Core.Const((:thread\_count,))  
│ %7 = Core.apply\_type(Core.NamedTuple, %6)::Core.Const(NamedTuple{(:thread\_count,)})  
│ %8 = Core.tuple(1)::Core.Const((1,))  
│ %9 = (%7)(%8)::Core.Const((thread\_count = 1,))  
│ %10 = VideoIO.openvideo::Core.Const(VideoIO.openvideo)  
│ %11 = Core.kwfunc(%10)::Core.Const(VideoIO.var"#openvideo##kw"())  
│ %12 = VideoIO.openvideo::Core.Const(VideoIO.openvideo)  
│ %13 = (%11)(%9, %12, %5, video\_path)::Core.PartialStruct(Tuple{UInt16, Any}, Any[Core.Const(0x0032), Any])  
└── return %13

Why is it type unstable? (Body::Tuple{UInt16, Any})
