Are enum-comparisons slow in MATLAB?

26 views (last 30 days)
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 Comments
Jan Siegmund
Jan Siegmund on 22 Mar 2020
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 on 22 Mar 2020
Code with 30mio calls to such a comparison: 30min with bare numbers, 2:15h with enum comparison. Thats a dealbreaker.

Sign in to comment.

Accepted Answer

per isakson
per isakson on 27 May 2020
Edited: per isakson on 27 May 2020
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 Comment
Joakim Näsström
Joakim Näsström on 24 Jun 2020
Edited: Joakim Näsström on 24 Jun 2020
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 :)

Sign in to comment.

More Answers (1)

Anurag Pratap Singh
Anurag Pratap Singh on 23 Jun 2020
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 Comments
per isakson
per isakson on 23 Jun 2020
"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 on 24 Jun 2020
Edited: Joakim Näsström on 24 Jun 2020
Switched to hardcoded integers instead, much faster since enumerations was not a viable option due to the performance hit.

Sign in to comment.

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!