Are enum-comparisons slow in MATLAB?
Show older comments
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
Dariusz Borkowski
on 24 Feb 2020
I also observed such behavior. It is visible not only in profiler but also in a normar execution. It is superslow. Shame for matlab.
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
on 22 Mar 2020
Code with 30mio calls to such a comparison: 30min with bare numbers, 2:15h with enum comparison. Thats a dealbreaker.
Accepted Answer
More Answers (1)
Anurag Pratap Singh
on 23 Jun 2020
0 votes
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
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
on 24 Jun 2020
Edited: Joakim Näsström
on 24 Jun 2020
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!