# Running time increase in for loop while making CVX problem

I am trying to solve a geometric problem with the CVX MATLAB package. The issue is that the running time increases with each iteration of for loop.
What should I do?

## The example code is the following:

M=100;K=40;
G = rand(M,K);
cvx_begin
variable x(M,K)
minimize(0)
subject to
for i =1:20
tic
for k=1:K
Ax = (G(:,k).*sqrt(x(:,k)));
end
toc
clear Ax
end
cvx_end

## And the output is as follows:

Elapsed time is 1.951600 seconds.
Elapsed time is 1.865454 seconds.
Elapsed time is 1.795588 seconds.
Elapsed time is 2.279985 seconds.
Elapsed time is 2.587530 seconds.
Elapsed time is 2.747588 seconds.
Elapsed time is 3.706303 seconds.
Elapsed time is 4.140957 seconds.
Elapsed time is 5.138583 seconds.
Elapsed time is 5.138122 seconds.
Elapsed time is 6.959450 seconds.
Elapsed time is 7.754655 seconds.
Elapsed time is 8.494337 seconds.
Elapsed time is 24.810320 seconds.
Elapsed time is 25.497016 seconds.
Elapsed time is 25.820188 seconds.
Elapsed time is 28.247370 seconds.
Elapsed time is 30.909417 seconds.
Elapsed time is 29.220745 seconds.
Elapsed time is 29.382676 seconds.

I have duplicated this (although with a less extreme increase in times on my computer). I don’t understand it. Perhaps something to do with CVX’s under the hood bookkeeping and processing, even though nothing is retained at the user level. This phenomenon occurs even if `Ax =` is removed, just keeping the RHS of the statement.

I suspect that only the CVX developer can authoritatively answer this.

Of course, the best advice is to try to vectorize as much as possible, and reduce the number of levels of for loop (for instance from 2 to 1), even if they can’t be eliminated.

Thanks for your suggestion.