How to make program run faster?

1 view (last 30 days)
Aleena
Aleena on 17 Oct 2013
Commented: Jan on 17 Oct 2013
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 Comments
Aleena
Aleena on 17 Oct 2013
Thanks for the answer.I think i confused you with my question.Let me make it more clear.My excel sheet has coloumn A with Alphabets A to Z.(these alphabets are repeated several times) Colummn B ignore (some non required data) Coloumn C has some numbers. Coloumn D has time 0000,0030,0100,0130...till 2330.(these alphabets are repeated several times)
Total of 65000 rows. I need to create a matrix so that the output is a array with dimension P[26,49]. I wanted to add value in column C,if column A and Column D in a row are equal to another row with same value for coloumn A and D in the entire excel sheets.I used '3' for loops to create matrix P[26,49]
Jan
Jan on 17 Oct 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.

Sign in to comment.

Answers (1)

Joe M
Joe M on 17 Oct 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!

Categories

Find more on Data Import from MATLAB 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!