Speed up this bottleneck line of code
Hello.
I have a big chunk of code, here is a part of it (arrays a, b and c, indeces ind1 and ind2 are actually different, its just an artificially constructed example):
n = 10000;
m = 5000;
k = 20;
a = rand(n,m);
b = rand(n,m);
c = rand(m+k,n)*1.1-0.1;
ind1 = repmat([ones(1,m/k),0]==1,1,k)’;
ind2 = repmat([0,ones(1,m/k)]==1,1,k)’;
tic;
d = m * sum(a .* b .* max(0, c(ind1,:) – c(ind2,:))’,2);
toc
I am not satisfied with it’s speed. After profiling my code i found that string
d = m * sum(a .* b .* max(0, c(ind1,:) – c(ind2,:))’,2);
is a bottleneck of my code. Indeces ind1 and ind2 are almost entirely true.
Now im not sure now to improve from here, i’ve tried to google how to speed up .* operation or speed up indexing of matrix c but with no luck. Any advice would be great.
Thanks!Hello.
I have a big chunk of code, here is a part of it (arrays a, b and c, indeces ind1 and ind2 are actually different, its just an artificially constructed example):
n = 10000;
m = 5000;
k = 20;
a = rand(n,m);
b = rand(n,m);
c = rand(m+k,n)*1.1-0.1;
ind1 = repmat([ones(1,m/k),0]==1,1,k)’;
ind2 = repmat([0,ones(1,m/k)]==1,1,k)’;
tic;
d = m * sum(a .* b .* max(0, c(ind1,:) – c(ind2,:))’,2);
toc
I am not satisfied with it’s speed. After profiling my code i found that string
d = m * sum(a .* b .* max(0, c(ind1,:) – c(ind2,:))’,2);
is a bottleneck of my code. Indeces ind1 and ind2 are almost entirely true.
Now im not sure now to improve from here, i’ve tried to google how to speed up .* operation or speed up indexing of matrix c but with no luck. Any advice would be great.
Thanks! Hello.
I have a big chunk of code, here is a part of it (arrays a, b and c, indeces ind1 and ind2 are actually different, its just an artificially constructed example):
n = 10000;
m = 5000;
k = 20;
a = rand(n,m);
b = rand(n,m);
c = rand(m+k,n)*1.1-0.1;
ind1 = repmat([ones(1,m/k),0]==1,1,k)’;
ind2 = repmat([0,ones(1,m/k)]==1,1,k)’;
tic;
d = m * sum(a .* b .* max(0, c(ind1,:) – c(ind2,:))’,2);
toc
I am not satisfied with it’s speed. After profiling my code i found that string
d = m * sum(a .* b .* max(0, c(ind1,:) – c(ind2,:))’,2);
is a bottleneck of my code. Indeces ind1 and ind2 are almost entirely true.
Now im not sure now to improve from here, i’ve tried to google how to speed up .* operation or speed up indexing of matrix c but with no luck. Any advice would be great.
Thanks! times, max, indexing, speed MATLAB Answers — New Questions