# Julia 1.0.2 reapeat function cost many times than matlab‘s repmat

**URL:** <https://discourse.julialang.org/t/julia-1-0-2-reapeat-function-cost-many-times-than-matlab-s-repmat/17708>\
**Category:** Internals & Design\
**Created:** [November 19, 2018, 9:54am UTC](https://discourse.julialang.org/t/julia-1-0-2-reapeat-function-cost-many-times-than-matlab-s-repmat/17708 "2018-11-19T09:54:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yang/32/5232_2.png) [@yang](https://discourse.julialang.org/u/yang)\
**Post date:** [November 19, 2018, 9:54am UTC](https://discourse.julialang.org/t/julia-1-0-2-reapeat-function-cost-many-times-than-matlab-s-repmat/17708/1 "2018-11-19T09:54:51Z")

</div>

I found julia’s repeat function run very slowly than matlab’s repmat and python too,here is my code of julia and matlab for a test of two functions,  
anyone if want test a python script can write it yourself or ask me to update one.  
the test contains smaller arrays and bigger,script and function,if any illogicality please point it out and i’ll try to write a correctly ones.

```julia
using Random
using BenchmarkTools

a1=bitrand(25,2500);
a2=bitrand(2500,2500);

(clen,alen)=size(a1);
ta=BitArray(zeros(1,alen));

@btime for i=1:20
    ta[1,:]=a1[i,:];
    A=repeat(ta,outer = [clen-i+1,1]);
end

(clen,alen)=size(a2);
ta=BitArray(zeros(1,alen));

@btime for i=1:20
    ta[1,:]=a2[i,:];
    A=repeat(ta,outer = [clen-i+1,1]);
end

function reptime(a)
    (clen,alen)=size(a);
    ta=BitArray(zeros(1,alen));
    A=similar(ta)
    for i=1:20
        ta[1,:]=a[i,:];
        A=repeat(ta,outer = [clen-i+1,1]);
    end
    return A
end

@btime reptime(a1);

@btime reptime(a2);

```

MATLAB code

```matlab
a1=logical(randi(2,25,2500)-1);
a2=logical(randi(2,2500,2500)-1);

tic
[clen,~]=size(a1);
 for i=1:20
    A=repmat(a1(i,:),[clen-i+1,1]);
end
toc

tic
[clen,~]=size(a2);
for i=1:20
    A=repmat(a2(i,:),[clen-i+1,1]);
end
toc

tic
reptime(a1);
toc

tic
reptime(a2);
toc

function A=reptime(a)
    [clen,~]=size(a);
    for i=1:20
        A=repmat(a(i,:),[clen-i+1,1]);
    end
end

```

![QQ%E5%9B%BE%E7%89%8720181119174741](https://global.discourse-cdn.com/julialang/original/3X/6/a/6a2d0eab1910c313e31bbed18f3e8b262cc34cd0.png)  
 ![matlab2017b](https://global.discourse-cdn.com/julialang/original/3X/c/2/c27ddf54b4a51b0a75eea6937310486afe52e4a4.png)  
also you can download those script on github  
[https://github.com/outyang/repeatandrepmat](http://repeatandrepmat)

---

<div class="post-metadata">

**Author:** ![yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yang/32/5232_2.png) [@yang](https://discourse.julialang.org/u/yang)\
**Post date:** [November 19, 2018, 10:57am UTC](https://discourse.julialang.org/t/julia-1-0-2-reapeat-function-cost-many-times-than-matlab-s-repmat/17708/2 "2018-11-19T10:57:33Z")

</div>

my report issues [issues](https://github.com/JuliaLang/julia/issues/30082)

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [November 19, 2018, 11:10am UTC](https://discourse.julialang.org/t/julia-1-0-2-reapeat-function-cost-many-times-than-matlab-s-repmat/17708/3 "2018-11-19T11:10:04Z")

</div>

There are a few issues with the Julia script. For example, You are pre-allocating and then replacing the object rather than using an in-place operation.

```julia
julia> using BenchmarkTools
julia> obj = rand(1_000);
julia> @benchmark repeat($obj, 50, 30)
BenchmarkTools.Trial: 
  memory estimate: 11.44 MiB
  allocs estimate: 2
  --------------
  minimum time: 1.728 ms (0.00% GC)
  median time: 2.897 ms (0.00% GC)
  mean time: 2.594 ms (27.99% GC)
  maximum time: 63.665 ms (96.94% GC)
  --------------
  samples: 1924
  evals/sample: 1

```

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [November 19, 2018, 12:11pm UTC](https://discourse.julialang.org/t/julia-1-0-2-reapeat-function-cost-many-times-than-matlab-s-repmat/17708/4 "2018-11-19T12:11:30Z")

</div>

Your

```julia
@btime for i=1:20
    ta[1,:]=a1[i,:];
    A=repeat(ta,outer = [clen-i+1,1]);
end

```

should better be something like

```julia
@btime for i=1:20
    A=repeat((@view a1[i,:])',outer = (clen-i+1,1));
end

```

because otherwise you are allocating all the time (array slices on the r.h.s of assignments allocate).

On my machine, your version takes

```julia
6.928 s (349010 allocations: 36.12 GiB)

```

whereas the view based version takes

```julia
966.492 μs (50050 allocations: 1.15 MiB)

```

which is about a 7000x speed up.

---

<div class="post-metadata">

**Author:** ![yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yang/32/5232_2.png) [@yang](https://discourse.julialang.org/u/yang)\
**Post date:** [November 19, 2018, 1:07pm UTC](https://discourse.julialang.org/t/julia-1-0-2-reapeat-function-cost-many-times-than-matlab-s-repmat/17708/5 "2018-11-19T13:07:01Z")

</div>

tks,view works well enought

---

<div class="post-metadata">

**Author:** ![yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yang/32/5232_2.png) [@yang](https://discourse.julialang.org/u/yang)\
**Post date:** [November 19, 2018, 1:57pm UTC](https://discourse.julialang.org/t/julia-1-0-2-reapeat-function-cost-many-times-than-matlab-s-repmat/17708/6 "2018-11-19T13:57:56Z")

</div>

i found view maybe should write in script but not function,so i update the julia ones code as follows:

```julia
using Random
using BenchmarkTools

a1=bitrand(25,2500);
a2=bitrand(2500,2500);

(clen,alen)=size(a1);
@btime for i=1:25
    A=repeat((@view a1[i,:])',outer = (clen-i+1,1));
end

(clen,alen)=size(a2);
@btime for i=1:25
    A=repeat((@view a2[i,:])',outer = (clen-i+1,1));
end

```

it’s run time on my machine is here,also a great speed up too.  
F:\>julia Untitled10.jl  
3.911 ms (200 allocations: 108.58 KiB)  
1.162 s (275 allocations: 18.55 MiB)
