I am getting more time complexity ,can you possibly minimize the code
1 次查看(过去 30 天)
显示 更早的评论
please review the attached code below,and give me some suggestions to reduce the time complexity
0 个评论
回答(6 个)
Rik
2018-2-22
for k = 1 : numel(h)
indexH = floor(8 * h(k));
indexS = floor(2 * s(k));
indexV = floor(2 * v(k));
h(k)=indexH;
s(k)=indexS;
v(k)=indexV;
end
Replace it with:
h=floor(8*h);s=floor(2*s);v=floor(2*v);
Another part you can optimize easily is this:
for i=1:72
A(i)=length(find (G==i));
end
You should replace this with something like histcounts, you might even not have to reshape your array.
Otherwise, use the profiler to find out what part of your code is the bottleneck and optimize/rethink that. You should be aware that reading from disk is very slow compared to the rest of the operation Matlab does. Also, you last loop doesn't do anything useful, but imshow is very slow. image is much faster in general, because it puts a lot of management task on you (clearing the axes when appropriate etc.).
4 个评论
Walter Roberson
2018-2-22
Rik is the person who mentioned "last loop". Rik was referring to
for z=1:999
for M=1:12
if (CP(z)==B(M))
subplot(5,3,M+3)
img = fullfile(pathname, sprintf('%d%s', z, str) );
imshow( img )
title( sprintf('z = %d', z) )
end
end
drawnow();
end
I explained below why this is a waste of time, that the functionality can be replaced by sort() and a simple loop.
The issue about imshow() is that imshow() does a lot of work on your behalf, some of which is not necessary to do if you know what you are doing with image() and calls to axes(). As your concern at this time is performance rather that design, you should replace
imshow( img )
with
image_data = imread(img);
image(image_data);
axis image equal
Walter Roberson
2018-2-22
You sort CP into B and then you do a double nested loop looking for places the 12 smallest sorted values equal the unsorted values. That is a waste of time. Instead when you do the sorting, use the two output form of sort() as that returns indices directly and no searches would be needed.
2 个评论
Walter Roberson
2018-3-1
Replace
for z=1:999
for M=1:12
if (CP(z)==B(M))
subplot(5,3,M+3)
img = fullfile(pathname, sprintf('%d%s', z, str) );
imshow( img )
title( sprintf('z = %d', z) )
end
end
drawnow();
end
with
[~, sortidx] = sort(CP);
for M = 1 : 12
z = sortidx(M);
img = fullfile(pathname, sprintf('%d%s', z, str) )
subplot(5,3,M+3)
imshow( img )
title( sprintf('z = %d', z) )
end
teja jayavarapu
2018-2-28
编辑:teja jayavarapu
2018-2-28
8 个评论
Jan
2018-3-2
@teja: Meanwhile I'm completely confused by this thread. I find some code, some explanations and some inputs distributes over the question, some answers and some comments. I suggest to open a new thread, include all requires information in the question, provide the code directly as text instead of an attachment, but attach the input data. Then add a link to this thread here and note, that the new thread is a clarification and summary.
teja jayavarapu
2018-3-5
2 个评论
Jan
2018-3-6
@Pavan teja: Who are you? This is the thread of teja jayavarapu, so why do you ask for help here?
Please do not make it hard to read your code. As you can see in the answers to your question, a nicely formatted code is much better to read. See http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup. Currently your code is such ugly, that it hurts to read it.
Walter and Rik have posted some suggestions already and you did not post a comment. Do the ideas work?
It is still not clear to me, what you are asking for. Do you want to simplify the code to make it nicer to read or to decrease the runtime?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!