I don't understand why a function is taking more than an hour whereas its sister function is taking only 4-5 minutes

2 visualizzazioni (ultimi 30 giorni)
Both the functions are very basic: Only difference is one line in bold in second code
  1. function taking 4-5 mins-
function international
year= input('Enter the year:'); %year
y=num2str(year);
fname= strcat(y,'.csv'); %finding excel of that year
[~,dep,~]=xlsread(fname,'G:G'); %reading departures and arrivals
[~,arv,~]=xlsread(fname,'M:M');
first= input('enter first country:');
second= input('\nenter second coutnry:');
index=find(strcmp(dep,first) & strcmp(arv,second)); %indices of couple
database=cell(10000,59);%allocating memory
for c=1:size(index) %loop for importing all data
q=int2str(index(c));
str1=strcat('A',q);
str2=strcat('BG',q);
[~,~,database(c,:)]=xlsread(fname,'',[str1,':',str2]);
end
avgasm= mean(cell2mat(database(:,59))); %average asm
fprintf('Average seat miles:%f\n',avgasm);
filename='result.xlsx';
xlswrite(filename,database(:,[2 3 5 7 11 13 32 59]));%printing excel with important data
end
2. Function taking more than an hour:
function domestic
year= input('Enter the year:'); %year
y=num2str(year);
fname= strcat(y,'.csv');%finding excel of that year
[~,dep,~]=xlsread(fname,'G:G'); %reading departures and arrivals
[~,arv,~]=xlsread(fname,'M:M');
first= input('enter the country:');
*index=find(strcmp(dep,first) & strcmp(arv,first));* %indices of couple
database=cell(10000,59); %allocating memory
for c=1:size(index) %loop for importing all data
q=int2str(index(c));
str1=strcat('A',q);
str2=strcat('BG',q);
[~,~,database(c,:)]=xlsread(fname,'',[str1,':',str2]);
end
avgasm= mean(cell2mat(database(:,59))); %average asm
fprintf('Average seat miles:%f\n',avgasm);
filename='result.xlsx';
xlswrite(filename,database(:,[2 3 5 7 11 13 32 59])); %printing excel with important data
end
  3 Commenti
Mihir
Mihir il 13 Feb 2017
Modificato: Mihir il 13 Feb 2017
Thank you, In international, xlsread should read all data of those rows which have G and M column as input by person. In domestic, xlsread should read all data of those rows whose BOTH G and M column is same.
dpb
dpb il 13 Feb 2017
The question is, what do size(dep) and size(arv) show for the two cases?
As for the debugger, there's a whole section on using it in the documenation under the section on "Programming Scripts and Functions". Type
doc matlab % at command line, follow links...

Accedi per commentare.

Risposta accettata

Jan
Jan il 13 Feb 2017
Modificato: dpb il 13 Feb 2017
If most of the time is spent in xlsread, this is not a Matlab problem, but it concerns Excel. While the Matlab codes might need almost the same amount of time, the observed delay happens inside Excel. And this can neither be examined in detail or influenced from Matlab.

Più risposte (1)

John D'Errico
John D'Errico il 13 Feb 2017
Modificato: John D'Errico il 13 Feb 2017
The two functions are identical as far as I can see except for their names, and a few lines with one more spare space character, which will affect nothing. The one line that you starred, is not obvious as a significant difference, because we are not able to know anything about your data, how you run this code.
The difference lies in what you pass in as inputs, and the data they read. It is highly likely that the variance lies in those differences. Those are things we cannot know. Files of different sizes will take more time to process. Can you possibly be surprised?
If you really want to understand what is happening, then you need to learn to use tools like the debugger and the profile tool. But only you can do that.
  4 Commenti
dpb
dpb il 13 Feb 2017
Modificato: dpb il 13 Feb 2017
"[on] profiling, the major time is spent of xlsread"
As Jan points out, that's more opaque and less related to Matlab than Excel if that's the case. Perhaps the second spreadsheet has something funky going on about it; maybe it somehow has a humongous number of rows that aren't necessarily of interest or perhaps again there is just that much more data.
xlsread/xlswrite are definitely not speed demons in their operation; you might try creating a clean workbook and save only the data really need and see if makes any improvement.
The real answer may be to save the data out of Excel into a .csv file instead, thereby foregoing xlsread so can read the data in a more direct manner.
Mihir
Mihir il 14 Feb 2017
Thanks a lot! Well i substantially reduced time by using readtable. Yes indeed the problem was in excel. Just adding, that excel file has 256,000 rows. I am good now. :)

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by