Which function will cause less load to Matlab?
古いコメントを表示
By load, I mean the processing time Matlab needs.
Having 3000 lines of if statements where the statement is constant and only the condition is varied or to use a while(1) with a loop with a evalc and sprintf
while(1) is needed in the loop because it needs to be compared all the time.
採用された回答
その他の回答 (1 件)
per isakson
2014 年 3 月 10 日
編集済み: per isakson
2014 年 3 月 13 日
Both alternative appears a bit problematic
- 3000 lines littered with if-statements sounds difficult to get right and maintain
- eval and sprintf add overhead and Why is it advised to avoid using the "eval" function?
Make an experiment. There is a button [Run and Time].
There must be a third way. Give us a little more background.
.
Step 1. Data structure. What output do you want from your program? That is important to know to choose an appropriate data structure. Maybe, I'm guessing, a "nested" Matlab struct array. "ON" is represented by true and OFF by false. These lines initiate such a struct
clear('day','room')
day = struct( 'slot', false( 1, 12 ) );
room( 1, 120 ) = struct( 'day', repmat( day, [1,7] ) );
Now each one of the "10,080" slots can be accessed in a logical way, e.g.
>> room(45).day(5).slot(9)=true
room =
1x120 struct array with fields:
day
>> room(45).day(5).slot(9)
ans =
1
What is the input (ON/OFF from the rooms) to the system and what kind of output is required? "Having 3000 lines of if statements [...]" is that an m-file or some kind of output from a building automation system?
.
A timing experiment:
Evaluating the button-color for
- 8 buildings
- 4 floors
- 30 rooms
takes less than 0.04 seconds with my five years old vanilla desktop.
.
All the slot information is stored in a structure array:
Building(8).Floor(4).Room(30).TimeSlot
The field TimeSlot holds all time slot data for one week. (I have not included the check-boxes - yet.)
Initiate one time_slot_array with some simple data
vec = datevec( '2014-03-03 00:00:00' );
time_slot_array = nan( 12, 2, 7 ); % time_slot_array(:,1,:) 1 is begin, 2 end
ssn = @(sdn) round(24*3600*sdn);
iso_week_day = @(sdn) 1 + rem( weekday( sdn - 2 ), 7 );
for iwd = 1 :7
for ts = 1 : 12
time_slot_array(ts,1,iwd) = ssn(datenum( vec + [0,0,iwd-1,7+ts, 0, 0] ));
time_slot_array(ts,2,iwd) = ssn(datenum( vec + [0,0,iwd-1,7+ts,59,59] ));
end
end
Assign the same time_slot_array to all rooms
Building(8).Floor(4).Room(30).TimeSlot = time_slot_array;
is_red = false( 4, 4, 30 );
for bb = 1 : 8
for ff = 1 : 4
for rr = 1 : 30
Building(bb).Floor(ff).Room(rr).TimeSlot = time_slot_array;
end
end
end
Evaluate red/green for all rooms (all will get the color)
cur_sdn = datenum( '2014-03-05 10:52:00' ); % current time
cur_ssn = ssn( cur_sdn );
tic
for bb = 1 : 8
for ff = 1 : 4
for rr = 1 : 30
iwd = iso_week_day( cur_sdn );
is1 = cur_ssn >= Building(bb).Floor(ff).Room(rr).TimeSlot(:,1,iwd);
is2 = cur_ssn <= Building(bb).Floor(ff).Room(rr).TimeSlot(:,2,iwd);
is_red( bb, ff, rr ) = any( is1 & is2 );
end
end
end
toc
outputs
Elapsed time is 0.032368 seconds.
18 件のコメント
ericson
2014 年 3 月 10 日
ericson
2014 年 3 月 10 日
Walter Roberson
2014 年 3 月 10 日
You should be using indexing. For example,
weekdays = {'sun', 'mon', 'tue', 'wed', 'th', 'fr', 'sat'};
word = weekdays{k};
per isakson
2014 年 3 月 10 日
編集済み: per isakson
2014 年 3 月 10 日
These are the strings, which are evaluated with evalc in the first execution of the inner loop.
temp = load('a1_compare.mat')
a1mon1 = data.a1mon1.string
a1mon1 = str2num(a1mon1)
fa1mon1 = data.fa1mon1.string
fa1mon1 = str2num(fa1mon1)
Most of the characters in the variable names are in effect indices. How do you plan to use these variables? There will be many!
Questions
- count and loop are they "group of buildings" and "building in group"?
- f is that "slot"?
a1mon1 stands for
- building group "a"
- building "1" (in group "a")
- day of week "mon"
- slot "1"
Am I right in my guessing? So far the names of the variables. The values are converted from string to numerical.
This code is neither efficient nor programmer friendly. However, more information is needed to propose something better.
More questions
- Are there 120 mat-files, one for each building?
- What is the structure of the content of the mat-files?
- Names such as a1mon1 are used in the mat-files?
- Every mat-files uses different names for the variables?
ericson
2014 年 3 月 11 日
per isakson
2014 年 3 月 11 日
編集済み: per isakson
2014 年 3 月 11 日
- In your problem domain you have buildings, floors, rooms, Used/Vacant (ON/OFF), Week, Day Hour. It would make programming and maintenance much simpler if those words are reflected in the names of the variables.
- You use a complicated way of "indexing" the variables. Matlab offer good support for indexing, e.g. I find floor(1) better than "a" in position one.
- Your original question was about efficiency. I think that the major issue regarding efficiency is the communication between the GUIs is via many small mat-files. Wouldn't callbacks be more efficient? However, is efficiency a problem?
- Your program will spend most of the time checking mat-files, which have not been changed since the last check. Is that so?
ericson
2014 年 3 月 11 日
per isakson
2014 年 3 月 11 日
編集済み: per isakson
2014 年 3 月 11 日
You have two GUIs
- edit_time. Is there one per room. It has one push-button, Apply. A user edits the schedule and presses [Apply]. "NaN" what is that?
- PROGRAM. Change Time and Change Room Name. "Select" is that not a better word than "Change".
There is one mat-file, ??_compare.mat, for each building?
The structure of the data will affect the execution time. "[...]like floor(x).room(y).date(i).slot(z)?" Tentatively, I would say
building(.).floor(.).room(.).sdn
building(.).floor(.).room(.).slot
where sdn is a row vector of "serial date number" or other time and slot i a array [2x12xlength(sdn)]. With Matlab one should not fragment data "too much". slot is that a period of time with minute resolution?
Before deciding on the data structure one should analyze how data will be accessed.
ericson
2014 年 3 月 12 日
per isakson
2014 年 3 月 12 日
編集済み: per isakson
2014 年 3 月 12 日
I have problems understanding how this tool will be used; which the requirements are. I think of it as a "booking system". Or is it rather a system to report whether rooms are actually used.
What does the color of the room button exactly mean? Does a red button mean that the room is being used "at the moment", i.e 10:31:54.
What happens when the [Apply]-button is pressed?
Each "slot" has a starting and an ending point in time together with a check-box according to the GUI. Why call it "slot". "time slot" make me think of predefined non-overlapping periods of time.
I cannot understand why there is a problem with "computer load", which indicates that I don't understand the requirements.
ericson
2014 年 3 月 12 日
per isakson
2014 年 3 月 12 日
編集済み: per isakson
2014 年 3 月 12 日
In the edit_time GUI, is the user supposed to fill in time in the edit boxes, which show the value "00:00-00:00"? Or are the start end end times of the slots predefined?
What happens when the [Apply]-button of the edit_time GUI is pressed?
ericson
2014 年 3 月 13 日
per isakson
2014 年 3 月 13 日
It's more effective to have discussions like this one in front of a white-board. "I don't get it, where will I use the sdn for? is it different from the slot?". My problem is that I don't get it (appropriate smiley).
I understand that
- the edit_time GUI is only used from the PROGRAM GUI. (I did imagine some kind distributed system where the user of the room used edit_time to book a room.)
- there is a way to select a specific building, which is not obvious from the screen clips.
- "[...] editing the time slots [...]". I interpret, for each time slot the user types start and end time and finally "checks the box". If so, there is a need for asserting that time slots do not overlap. Are old definitions of time slots saved?
- rooms can only be booked for the current week - not for the whole semester.
What is the reason you have so many different mat-files? (In data base design something called ACID is important.) Saving and loading data of these files are likely what make your system choke.
I have read the thread and highlighted following:
"after all the variables are generated, it will then be compared to the time. If the current time is in between the time specified of the user having the same date (mon, tues, etc.), then the background color of the push button will change"
"[...] continuously being compared"
"let say var=12:00 and fvar=13:00, the current time should be in between of it so that the background color of the pushbutton will turn to red."
"in one building there are 240 mat files, 2 mat files per room(1 for the schedule and 1 for the comparing of the schedule)"
.
The simple solution to your efficiency problem might be
- use mat-file format '-v6'. See MAT-File Versions
- replace the while-loop by a timer. See timer class, Create object to schedule execution of MATLAB commands
The not so simple solution is to keep the GUIs and rewrite most of the code.
ericson
2014 年 3 月 13 日
per isakson
2014 年 3 月 13 日
編集済み: per isakson
2014 年 3 月 13 日
"I can't think of an efficient code [...]" I'm convinced a comparison can be done once every second. However, that requires a different way of keeping the "slot information".
Despite that I asked several times you have not exactly defined "time slot". Without such definitions it is hard to contribute. The screen copies you uploaded shows 00:00-00:00. When and by who are the actual values set? Which constraints on the values are there.
ericson
2014 年 3 月 13 日
per isakson
2014 年 3 月 13 日
編集済み: per isakson
2014 年 3 月 13 日
I've added a timing experiment to the answer above. The structure Building may
- be made persistent in the main function.
- saved to a version '-v6' mat-file as needed
I represent time with floating-point integer to avoid rounding errors. ssn stands for serial second number.
カテゴリ
ヘルプ センター および File Exchange で Git in MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!