Can I make this function faster?

조회 수: 3 (최근 30일)
lianne
lianne 2014년 2월 7일
답변: J 2014년 2월 8일
I need the following function in a program, but it takes a lot of time to run. Does anyone know how I can make it faster? "meting" can be as large as 25000x1 cell, every cell consists of (max, mostly less) 1x2000 char.
if true
% code
function [meetwaarde,power]=databewerking(meting,multiplier,offset)
string=cell2mat(meting);
hexa=regexp(string(1,:),'\w{1,2}','match');
lengtekolom=size(meting,1);
for i=2:lengtekolom
var=regexp(string(i,:),'\w{1,2}','match');
hexa=cat(1,hexa,var);
end
meetaantal=size(meting{1},2)/2;
meetwaarde=zeros(lengtekolom,meetaantal);
deca=hex2dec(hexa);
dec=reshape(deca,lengtekolom,meetaantal);
for i=1:lengtekolom
for j=1:meetaantal
if dec(i,j)==0
meetwaarde(i,j)=0;
else
meetwaarde(i,j)=(multiplier*dec(i,j))-offset;
end
end
end
power=sum(meetwaarde,2);
end
end
Thanks a lot!!
  댓글 수: 2
Patrik Ek
Patrik Ek 2014년 2월 7일
편집: Patrik Ek 2014년 2월 7일
And what about the code above. Does that not give you an error? Anyway in MATLAB you should always have the function definition
function out = myFunction(in)
on the top. Alternally you could have subfunctions below that is called by the "main function" (the first function defined on row 1). However, you cannot call a function written earlier in the code from a row below. Also you can then only call the "main function" from another .m file.
lianne
lianne 2014년 2월 7일
If true isn't in the actual code, it is just to show the code correctly (if you click on {}Code, you get this ;) )

댓글을 달려면 로그인하십시오.

채택된 답변

J
J 2014년 2월 8일
Your code is slow because you continuously grow a cell array within this line:
hexa=cat(1,hexa,var);
Since the cell-array is growing every loop, Matlab has to re-alloacate a new memory location for the cell array within each loop. Using vectorized code for this would significantly speed up your code. I think the following code gives the exact same result as your code, but is much faster. For a 5000x1 cell array with 1x2000 char I obtain a speed up of 500x on my laptop.
function [meetwaarde,power]=databewerking(meting,multiplier,offset)
lengtekolom=size(meting,1);
meetaantal=size(meting{1},2)/2;
string=cell2mat(meting);
string = reshape(string.', 2, numel(string)/2).';
deca=hex2dec(string);
dec=reshape(deca,meetaantal,lengtekolom).';
meetwaarde=zeros(lengtekolom,meetaantal);
meetwaarde(dec ~= 0) = (multiplier*dec(dec ~= 0))-offset;
power=sum(meetwaarde,2);

추가 답변 (1개)

Dina Irofti
Dina Irofti 2014년 2월 7일
Have you tried parallel for loop? See parfor .
  댓글 수: 1
Patrik Ek
Patrik Ek 2014년 2월 7일
Parfor seems to be a bit of overkill in this case, since it seems the most of the code could be vectorized. Also, the code takes much time to run and no matlabpools are available, eg if the code is run on the personal laptop, then you may not want to occupy all cores at the same time.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by