MATLAB Answers

Assigning values to the array of Enumeration Class seems to be very slow

조회 수: 16(최근 30일)
pingping zhang
pingping zhang 20 Jan 2020 0:34
댓글: pingping zhang 20 Jan 2020 4:40
Hi, I met this problem when parsing a gps log.
I made an Enumeration Class GpsState as follows.
classdef GpsState
enumeration
Lost
Single
RTD
RTK_Float
RTK_Fix
end
end
Then I created an array of GpsState to record all the states of the log. The process was painfully slow.
Finally, I found out what the problem was caused by the assign of Enumeration Class array.
I simplified the code as follows, the process takes about 4.6s.
tic
test_num = 1e5;
gps_state = repmat(GpsState.Lost,test_num,1);
for ii = 1:test_num
gps_state(ii) = GpsState.RTK_Fix;
end
toc
Is there any problem with my usage mode of Enumeration Class?
Thanks for your reading.

  댓글 수: 0

로그인 to comment.

답변(1개)

Steven Lord
Steven Lord 20 Jan 2020 3:45
Why are you initializing all the elements in gps_state to GpsState.Lost in one call them immediately overwriting them with GpsState.RTK_Fix?
If you oversimplified your problem, and you wanted say) to only overwrite certain elements of gps_state, there's no need for a for loop here. Just overwrite them in one fell swoop, something like this untested code.
test_num = 1e5;
gps_state = repmat(GpsState.Lost, test_num, 1);
OneThousandRandomPoints = randperm(test_num, 1000);
gps_state(OneThousandRandomPoints) = GpsState.RTK_Fix; % scalar expansion

  댓글 수: 1

pingping zhang
pingping zhang 20 Jan 2020 4:40
Hey, there is some misunderstanding,that is my issue.
In actual fact, the original code contains a process of parsing and judging gps status.
I read gps status words from the log line by line, and assign gps_state values one by one.
It's something like this.
for ii = 1:test_num
....
switch gps_status_word(ii)
case "00"
gps_state(ii) = GpsState.Lost;
case "04"
gps_state(ii) = GpsState.RTK_Fix;
...
end
end
I found the process was particularly slow, and the root cause is the the assign of Enumeration Class array.
If I replace the Enumeration of GpsState by another tye like int8, for exmaple, 'gps_state(ii) = 0' , it will be much faster.
In order to describe the problem more simply, I use the code above. It shows us the elapsed time spent when assigning values to the array of Enumeration Class directly.
Now I can temporarily use int8 instead, but still want to make sure how to use Enumeration Class effectively.
Thanks for your advice.

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by