Why does MATLAB resample function get 5 times slower when converted to C++?
10 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I am using MATLAB 2020b and I need to use resample function in some parts in my code. In MATLAB, resample function runs very fast, but when I convert it to cpp and run the code in Visual Studio 2015 with the same inputs, it gets slower.
For a sample code like this:
y = 1/(5e6):1/(5e6):1
tic
p = 8;
q = 5;
for i = 1:10
y1 = resample(y, p, q)
end
elapsed_time = toc;
The elapsed time is measured as 1.6 seconds in my computer. However, in Visual Studio, it is 10 seconds. The result y1 is same in both codes.
Actually, I might think that it is related to MATLAB Coder Generate settings, but my own resample code formed with the same configuration suprisingly outperforms the MATLAB function in cpp version, altough I use only loops for the calculations instead of vectorized operations (It is assumed to be very slow).
I want to mention that I could not convert MATLAB resample function to cpp in MATLAB 2019b. It gives an error like "coder.const could not reduce this expression to a constant". The solution proposed in the link below didn't work for me and I setup MATLAB 2020b.
Still, I believe that this happened because of my inexperience and I would appreciate if you can share your precious thoughts.
0 件のコメント
回答 (1 件)
Chidvi Modala
2021 年 3 月 30 日
Built-in MATLAB functions are highly optimised for the PC, and exploit multithreading parallelism. The C code generated with MATLAB Coder, on the other hand, is a readable and portable code which is not optimised for a particular platform. This explains why the slowdown is expected.
As a general note, compiled code is not always faster than MATLAB code, and built-in functions represent some of those cases.
However, there are some optimisation strategies that allow to boost the execution speed of the generated code. For further details, you may refer to the following link:
参考
カテゴリ
Help Center および File Exchange で Single-Precision Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!