Ideas
Follow


How to make corrplot from Econometrics Toolbox 2x faster?

Jan Studnicka on 7 Dec 2023 (Edited on 11 Dec 2023)
Latest activity Reply by Jan Studnicka on 26 Jan 2024

Quick answer: Add set(hS,'Color',[0 0.4470 0.7410]) to code line 329 (R2023b).
Explanation: Function corrplot uses functions plotmatrix and lsline. In lsline get(hh(k),'Color') is called in for cycle for each line and scatter object in axes. Inside the corrplot it is also called for all axes, which is slow. However, when you first set the color to any given value, internal optimization makes it much faster. I chose [0 0.4470 0.7410], because it is a default color for plotmatrix and corrplot and this setting doesn't change a behavior of corrplot.
Suggestion for a better solution: Add the line of code set(hS,'Color',[0 0.4470 0.7410]) to the function plotmatrix. This will make not only corrplot faster, but also any other possible combinations of plotmatrix and get functions called like this:
h = plotmatrix(A);
% set(h,'Color',[0 0.4470 0.7410])
for k = 1:length(h(:))
get(h(k),'Color');
end
Jan Studnicka
Jan Studnicka on 7 Dec 2023
tic
A = rand(10);
figure
[h,ax] = plotmatrix(A);
% compare elapsed times of code with commented and uncommented line:
% set(h,'Color',[0 0.4470 0.7410])
for i = 1:length(A)
for j = 1:length(A)
if i~=j
lsline(ax(i,j));
end
end
end
toc
Mike Croucher
Mike Croucher on 25 Jan 2024
This is interesting. On my Windows 11 machine, R2023b I get
  • Elapsed time is 2.693017 seconds.(commented)
  • Elapsed time is 1.946635 seconds.(uncommented)
So not the 2x faster you see but enough to be interesting.
However, Using the R2024 pre-release I get times like the following for both cases
  • Elapsed time is 2.884284 seconds
So not only is it slower but your trick no longer seems to help. Have you got access to the pre-release? Can you try it there too please?
Jan Studnicka
Jan Studnicka on 26 Jan 2024
Hi Mike,
You are right. In R2024 pre-release this trick no longer helps. The reason is in the lsline function, which has changed between these versions. If you copy the lsline function from R2023b installation to your current folder, it's faster again.
PS: The code in my comment is not the one that is 2x faster. I apologize for the confusion. Almost 2x faster is corrplot(A) with 20x20 matrix A when you add set(hS,'Color',[0 0.4470 0.7410]) to the line 329 of corrplot. I tested on my Windows 10 machine, R2023b.
corrplot without setting of Color (original):
  • Elapsed time is 39.010938 seconds.
corrplot with setting of Color (trick):
  • Elapsed time is 21.563862 seconds.