フィルターのクリア

Are enum-comparisons slow in MATLAB?

26 ビュー (過去 30 日間)
Joakim Näsström
Joakim Näsström 2019 年 11 月 11 日
編集済み: Joakim Näsström 2020 年 6 月 24 日
Hi,
For everyone to know which MATLAB verison I am running , this is it: MATLAB Version: 9.5.0.944444 (R2018b).
My question is simple, Should i or should i not use enumerations in MATLAB if performance is important?
Im my code, i have the following object and enumeration defined
classdef Object < handel
properties
Dependency (1,1) DependencyType
end
end
classdef DependencyType < uint8
enumeration
NONE (0)
LOWER (1)
UPPER (2)
end
end
The class have a property called Dependency whouch should be of type DependencyType and DependencyType is an enumeration and a subclass of uint8.
In another function, not a member of the class, i have an if-statement that looks like follows
if obj.Dependency == DependencyType.NONE
...
end
When I am running the profiler, the if-statement line takes up 35% of the execution time of the function, the function is called 100000 ish times. I wonder if using enumerations in MATLAB is slow or if this is just an overhead of the profiler? I have not found any information regarding this while looking on the internet! I cant understand why it would be slow since there should not be any type conversions. So it must be due to profiling being turned on, but I cant be sure!
If this is not just some overhead from the profiler, then whats wrong. Is it constantly createing new DependencyType objects in the if-statement? Am I doing the comparison wrongly? Why does the comparison take so long time compared to the rest of the code that is way more computationaly demanding in the function.
Best regards
Joakim
  3 件のコメント
Jan Siegmund
Jan Siegmund 2020 年 3 月 22 日
Same here. The profiler shows massive time consumption in converting the enums to a number to finally compare it. I'll go back to regular integer comparison.
Jan Siegmund
Jan Siegmund 2020 年 3 月 22 日
Code with 30mio calls to such a comparison: 30min with bare numbers, 2:15h with enum comparison. Thats a dealbreaker.

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

採用された回答

per isakson
per isakson 2020 年 5 月 27 日
編集済み: per isakson 2020 年 5 月 27 日
Indeed, enumerations make slow code.
This is an anecdote about a code based on enumeration that proved useless because of poor performance and a happy end.
I created a class to classify characters of m-code. I made it an excerise in Finite State Machine. I used one enumeration class for State and another for Input. It was a joy to work with enumeration and the resulting code is very readable. Then came the shock, the execution time was just unacceptable. Profiling didn't really help much.
Happy End
With a minor effort I improved the speed of the code by more than two orders of magnitude. First I replaced the enumeration classes by value classes with constant properties. Second I had to do a few minor changes in the main class. Third, since the profiler now produced better information, I modified two red lines.
Thus, I replaced
classdef InputTypes < uint8
enumeration
null_input ( 0 )
single_quote ( 1 )
double_quote ( 2 )
opening_bracket ( 3 )
closing_bracket ( 4 )
delimiter ( 5 )
...
end
end
by
classdef InputTypes
properties ( Constant = true )
null_input = 0;
single_quote = 1;
double_quote = 2;
opening_bracket = 3;
closing_bracket = 4;
delimiter = 5;
...
end
end
and
classdef CharacterTypes < uint8
enumeration
null_statement ( 0 )
end_of_statement ( 1 )
code_outside_brackets ( 2 )
code_in_brackets ( 3 )
in_character_constant ( 4 )
in_string_constant ( 5 )
in_single_line_comment ( 6 )
end
end
by
classdef CharacterTypes
properties ( Constant = true )
null_statement = 0;
end_of_statement = 1;
code_outside_brackets = 2;
code_in_brackets = 3;
in_character_constant = 4;
in_string_constant = 5;
in_single_line_comment = 6;
end
end
I use R2018b. Accourding to what I read in the release notes the performance of enumeration has not been improved.
  1 件のコメント
Joakim Näsström
Joakim Näsström 2020 年 6 月 24 日
編集済み: Joakim Näsström 2020 年 6 月 24 日
Hi, since the answer came in late I had to do something bofore that. I ended up removing all enumerations and switched to use hardcoded integers instead. Using classes as you did with constant fields might have been a better solution but maybe for next code refactoring :)

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

その他の回答 (1 件)

Anurag Pratap Singh
Anurag Pratap Singh 2020 年 6 月 23 日
Hiii Joakim
I understand that you are trying to use enum in your code and are doubtful if that is slow
The underlying type of an enumeration is an integral type and the compiler converts the enum to an int type.So type conversions may take some time But that is neglibile or unnoticeabe
Please refer to https://www.mathworks.com/help/matlab/matlab_oop/enumerations.html enums documentation for more info on the enum classl
  2 件のコメント
per isakson
per isakson 2020 年 6 月 23 日
"So type conversions may take some time But that is neglibile or unnoticeabe." implies that I made a gross mistake in the use of enumerations, which is the background to my answer.
Joakim Näsström
Joakim Näsström 2020 年 6 月 24 日
編集済み: Joakim Näsström 2020 年 6 月 24 日
Switched to hardcoded integers instead, much faster since enumerations was not a viable option due to the performance hit.

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

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by