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


#1

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.

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

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
matlab2017b
also you can download those script on github
https://github.com/outyang/repeatandrepmat


#2

my report issues issues


#3

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> 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

#4

Your

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

should better be something like

@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

6.928 s (349010 allocations: 36.12 GiB)

whereas the view based version takes

966.492 μs (50050 allocations: 1.15 MiB)

which is about a 7000x speed up.


#5

tks,view works well enought


#6

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

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)