Matlab save with -append is quick the first time then slows down dramatically the second time

5 ビュー (過去 30 日間)
Aaron Hallquist
Aaron Hallquist 2022 年 4 月 13 日
回答済み: Suraj 2023 年 10 月 5 日
I'm trying to attach a last used timestamp to some caching in my code so that I can later quickly load in the lastused variable and delete cached entries that haven't been used in the last N days. While loading a single datetime object is quick as I would expect, it seems like save(cacheFile, 'lastused', '-append') is an operation that scales with the size of the file (which can get to GB or larger). Why is this the case?
Also, in my testing, it seems like the save with -append is NOT a problem the first time, but after that it does become file size dependent. Why?
Here is some sample code that I've been running for testing the timing:
% time to save the big file | this is expected to be large
data = rand(3000);
tic; save('test.mat','data'); time.save = toc;
% time to append the first item | this is quick as I would hope all appends are
lastused = datetime;
tic; save('test.mat','lastused','-append'); time.appendFirstTime = toc;
% time to append the second time | this becomes slow
lastused = datetime;
tic; save('test.mat','lastused','-append'); time.appendSecondTime = toc;
% time to append a new variable | still slow, this shows that overwrite isn't causing the issue
othervar = datetime;
tic; save('test.mat','othervar','-append'); time.appendOtherVar = toc;

回答 (1 件)

Suraj
Suraj 2023 年 10 月 5 日
Hi Aaron,
I understand that you’re using “save()” function in MATLAB with the “-append” flag to write to an existing “.mat” file.
I suggest that you use MAT-file version “v7.3” which runs with faster save times for the code you’ve provided. You can do use at the time of initially saving your ".mat" file, with the following command.
save('test.mat','data','-v7.3');
You can read more about MAT-file versions from the documentation: https://www.mathworks.com/help/matlab/ref/save.html#btox10b-1-version
I hope this helps!
Best regards,
Suraj.

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by