How to find the efficiency of a function?

2 views (last 30 days)
Hello, I have the following function:
function [ans] = sumdigits(N)
a = num2str(N);
sum_odd = 0;
sum_even=0;
ans=false;
for i = 1:2:size(a, 2)
sum_odd = sum_odd + str2double(a(i));
end
for j=2:2:size(a, 2)
sum_even = sum_even + str2double(a(j));
end
if sum_even>sum_odd
ans=true;
end
disp(ans);
end
end
I need to find the efficiency of this function and I would like to know how to find an efficiency of a function in general.
  1 Comment
Walter Roberson
Walter Roberson on 18 May 2018
To improve the efficiency, change the str2double(a(i)) call to be (a(i)-'0') and str2double(a(j)) is (a(j)-'0')

Sign in to comment.

Answers (1)

the cyclist
the cyclist on 17 May 2018
I'm not what you mean by "efficiency". Do you mean the time the program (or specific lines) take to execute? If so, then take a look at this documentation page about measuring code performance.

Categories

Find more on Colormaps 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!