Check if string matches many possibilities or check if doesn't match few possibilities.

2 ビュー (過去 30 日間)
Let's say I have a set of validated strings: {'B2D2', 'CPS4', 'R2D2', 'RB2D2', 'SB2D2', 'T2D2'}. And I want to proceed based on whether the input string matches any of the following: {'B2D2', 'R2D2', 'RB2D2', 'SB2D2'}. Which would be a faster invocation of strcmp()?
if any(strcmp(params.Results.eletype, {'B2D2', 'R2D2', 'RB2D2', 'SB2D2'}))
Or, check if it DOES NOT match the supplementary possibilites:
if ~any(strcmp(params.Results.eletype, {'CPS4', 'T2D2'}))
I ask this from a high-performance standpoint. I'm trying to cut computation time wherever I reasonably can here.

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 11 月 2 日
編集済み: Ameer Hamza 2020 年 11 月 2 日
I guess that 2nd should be faster because it involve less comparisons; however, Instead of speculations, it is better to just profile the code: https://www.mathworks.com/help/matlab/matlab_prog/profiling-for-improving-performance.html.
  1 件のコメント
Christopher Wong
Christopher Wong 2020 年 11 月 2 日
Thank you for your answer. I suppose that I agree with your suggestion.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by