Big workspace = slow CVX (regardless of problem size)?

I noticed the following behavior in CVX: it report as solved my little optimization problem almost immediately (<1 sec), but take up to 20 seconds to return the answer.

After poking around with the profiler I determined it was spending all its time in cvx_pop. I’m not entirely sure what this function does but it was running a recursive call to cvx_id on every variable in the workspace, presumably to determine if these were CVX variables or not. The problem is that I have some very large datasets in the workspace that have a deep tree-like structure.

As a temporary fix, I added the following lines, starting at 113 of cvx_pop.m:

s1 = evalin( 'caller', 'who' );
if ~isempty(CVX_EXCLUDE)
    for ii = 1:numel(CVX_EXCLUDE)
        sidx = strmatch(CVX_EXCLUDE{ii}, s1);
        s1(sidx) = [];

What this does is exclude any variables from the recursive call that I put in the global CVX_EXCLUDE. I put my dataset variables in there and this fixed the problem for me; it now runs <1sec to solve the problem and return the solution.

I’m posting this here to ask if this behavior is a bug or (what I assume) default behavior, and if my fix will cause any possible problems for me in the future (assuming I never exclude any variable that overlaps with any variable used in between cvx_begin and cvx_end.) Also in case anyone else is having similar trouble.

1 Like

Alas, CVX does a lot of weird things in order to integrate itself so smoothly with the MATLAB environment.

Have you wondered, for instance, how it is possible that, after cvx_end is typed and the solver is called, that all of the variable expressions you have created are replaced with their computed numerical values? Well, it is because CVX searches the entire workspace for instances of CVX objects and replaces them. As you have seen, if you have a lot of other variables in your workspace, especially cell arrays and structures, this can take awhile. I wish there was an easy way to identify only those objects in the workspace of a particular type, but I cannot.

You’ve done a great job of diagnosing your problem, and I don’t think that your code will cause any problems for you.