How to make program run faster?
Mostrar comentarios más antiguos
I am writing a program to extract huge data from excel, about 65000 rows nd 4 columns per sheet.I extracted the data with xlsread and used for loop to link several sheets.I wanted to add column C,if column A and Column D in a row are equal to another row in the whole excel sheets.I used for loop to do this. When i run the progrm it takes hours.How should i improve my program? I tried using parfor, still taking time. Is there any alternative method?
2 comentarios
Aleena
el 17 de Oct. de 2013
Jan
el 17 de Oct. de 2013
It would be a good idea to post the relevant part of the program. Otherwise suggesting any modifications is wild guessing only.
The profiler reveals the bottlenecks of your program. You should focus on them, because it is not useful to accelerate code, which takes only 1% of the total processing time.
Respuestas (1)
Joe M
el 17 de Oct. de 2013
%construct 5x4 for illustration
A = [1;3;5;7;9];
B = [2;4;6;8;10];
C = [1;2;3;4;5];
D = [1;3;6;8;9];
%view matrix E:
E = [A B C D]
%filter rows of E where A == D, then add values
%from these rows of C, to second column of E:
E(:,2) = (A == D).*C + E(:,2);
%check that this works (view E again):
E
%Now construct a 65,000 x 4 matrix and compute a max
%execution time on it (should be done several times,
%to get an average execution time):
A = rand(65000,1);
B = rand(65000,1);
C = rand(65000,1);
D = A;
E = [A B C D];
tic
E(:,2) = (A == D).*C + E(:,2);
toc
%I get: Elapsed time is 0.001832 seconds for the single operation
%Now try it over 100 imported sheets:
%construct 100 instances of the 65,000x4,
%(corresponding to 100 "imported Excel sheets"):
for m = 1:100
F(m).data = [A B C D];
end
%now execute the match-and-add operation on all 100 sheets,
%and clock total execution time:
tic
for m = 1:100
F(m).data(:,2) = ...
( F(m).data(:,1) == F(m).data(:,4) ).*F(m).data(:,3) + F(m).data(:,2);
end
toc
%I get Elapsed time is 0.225642 seconds for the operation
%over 100 "imported Excel sheets". Your mileage may vary!
Categorías
Más información sobre Data Import from MATLAB en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!