Same code works on my computer but not on my friends'

4 次查看(过去 30 天)
Hello,
I am having an strange problem: I have Matlab R2012a installed on my own computer and Matlab R2009a (multi thread) installed on my friend's computer. Because of the reason that my computer has only 2gb ram whereas my friend's has 128gb and a better processor, I decided to execute my code on my friend's computer.
I connected to his computer with a remote desktop connection program, downloaded my code and dataset from dropbox and executed my code. However, my code's execution did not finish even though an hour passed. (It took about 20 minutes on my computer and if you think that my friend's system is much more better than mine, it should have taken less than 20 minutes.) Then I waited a few hours more but execution did not finish.
Then, to understand where the problem is, I evaluated the code step by step and noticed that it could not finish the execution of the following loop:
l=1;
for n=1:max_t
for m=1:t(1,n).numberofPoints
x_t(l)=t(1,n).matrix(m,1);
y_t(l)=t(1,n).matrix(m,2);
z_t(l)=t(1,n).matrix(m,3);
l=l+1;
end
end
minx = min(x_t(:));
miny = min(y_t(:));
minz = min(z_t(:));
It is just a simple loop to obtain my minimum point in x,y,z coordinates. I know I do not need loops to obtain maximum and minimum points of a struct but I am new to MATLAB and do not know much. Therefore it seemed like the easiest way to me.
I really have to be able to use my friends' computer because my ram will not be enough for the further process. Therefore I need to solve the problem. Could you please help me? What can be the reason that same code works on my computer but not on my friends' computer?
Regards
  3 个评论
Amadeus
Amadeus 2012-12-6
编辑:Amadeus 2012-12-6
I thought that it could be related to the different compiler versions on different matlab versions, but of course I am not sure.

请先登录,再进行评论。

采纳的回答

per isakson
per isakson 2012-12-6
编辑:per isakson 2012-12-6
R2012a might handle your code more efficiently than R2009a. There is something called "just in time compiler", JIT, which The Mathworks improve with each release. Others know more about the details. However, your code might run faster on your computer.
Put
if rem(n,10) == 0
disp( [ num2str(n), ' -- ', datestr( now ) ] )
end
or something similar in the outer loop to see what's going on.
BTW: are you sure ram is a bottleneck?
  2 个评论
Amadeus
Amadeus 2012-12-6
For my PC, bottleneck is the ram. Because I keep getting out of memory errors.
per isakson
per isakson 2012-12-6
编辑:per isakson 2012-12-6
So ram is a bottleneck, nevertheless you need to analyze what's going on your friends computer. Maybe, you should try to use the function, profile. It's good.

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2012-12-6
编辑:Jan 2012-12-6
As far as I can see you did not pre-allocate the variable x_t. Matlab 2012b has some smart methods to reduces the dramataic slowdown causes by this, but I'm not sure if this has been introduced in 2012a already.
Try this for an implicit pre-allocation:
n = sum([t.numberofPoints]);
k = n;
for n = max_t:-1:1
for m = t(1,n).numberofPoints:-1:1
x_t(k)=t(1,n).matrix(m,1);
y_t(k)=t(1,n).matrix(m,2);
z_t(k)=t(1,n).matrix(m,3);
k = k - 1;
end
end
You find a lot of explanations in this forum, when you search for "pre-allocation".

类别

Help CenterFile 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!

Translated by