MATLAB Answers


Same code, Different results on 3 computers - only simple multiplications and additions involved

Asked by cr
on 9 May 2013

I have seen questions and answers posted earlier on why results vary with different versions of OS and libraries, but my problem is peculiar: it has got only iterative operations involving simple additions and multiplications. Code shown below. It involves randn, but I found all 3 versions producing the same random vector. Time taken to run also hugely differs although computer specs are comparable.

alpha = 0.05;beta = -1;  delta = 1;gama=1;sigma = sqrt(.1);
xzero = .1;yzero =.1;zzero =0;
T = 2000;
dt = 0.002;    
N = T/dt;
randn('seed',66239); % use this for same set of random values.
Nsamp = 1;
dW = sqrt(dt)*randn(Nsamp,N);   %Generate array of Brownian movments
X = zeros(N+1,1);Y = zeros(N+1,1);Z = zeros(N+1,1);
X(1) = xzero;Y(1) = yzero;Z(1) = zzero;
for j =1:N
   Winc(j) = dW(:,j); 
tt = [0:dt:T];
index = fix(0.5*N);
DISP = sum(X(index:end,:).^2)/length(X(index:end,:))
VEL = sum(Y(index:end,:).^2)/length(Y(index:end,:))

On Computer 1 (MACI64,2.5Ghz i5, R2012b):

>> tic, EM_Duffing_Revised, toc
  DISP =
  VEL =
  Elapsed time is 1.767654 seconds.

On Computer 2: (MACI64, 2.4Ghz Core2Duo, R2010a):

>> tic, EM_Duffing_Revised, toc
Elapsed time is 7375.1905 seconds.

On Computer 3: (PCWIN, 2.67Ghz Core-i5, R2010a):

>> EM_Duffing_Revised

(time taken is ~ 2700 seconds).

I have another piece of code which does the same thing in a different way (with matrix calcs) and even that one has this weird behaviour. Firstly, what explains the difference in results. Secondly, how come the time taken differs so much. No way Comp1 is 1300 times faster than Comp3. Am I missing something very basic ? Thanks.


FYI: Win8, 1.70GHz, Core-i5, R2013a:

Elapsed time is 4.784460 seconds. 
DISP = 0.8645
VEL = 0.3431

I can't tell you why the numbers are off on each one, but as MATLAB upgrades its software, they tend to optimize performance which would explain why your fastest time is from the most recent update.

A similar question was raised earlier about whether updates ran faster

on 9 May 2013

Thats what perplexes me. My Comp1 does it in 1.7s and comp2 in 7300s. Anyways, what answer do you get. What os are you on?

2 Answers

Answer by Philip Borghesani on 9 May 2013

The performance difference is due to an optimization for array growth made in R2011a. Adding


Before the for loop makes all MATLAB versions have similar times.

I am not sure about the different results. I ran a few versions on Windows and got the same answer posted here for Windows machines each time.


Answer by Matt J
on 9 May 2013
Edited by Matt J
on 9 May 2013

Just some guesses. First, what does the profiler say? Second, you should try changing

    Winc(j) = dW(:,j);

to Winc(j) = dW(j) to see if the index parsing accounts for the differences in speed. Index parsing has changed drastically in performance between different versions of MATLAB. The line as you have it will produce an error anyway when Nsamp>1 and needs to be fixed. Better still would be to remove it from the loop altogether and just do Winc=dW.

As for the difference in results, did you verify by direct inspection that dW was the same on all machines? Aside from that, it is not clear how numerically stable this will be


If it's some kind of inverse filter, it could be a very singular inversion and very insensitive to round-off errors. What you call "simple additions and multiplications" are not really all that simple. Even addition is not numerically stable unless all the operands are the same sign.

  1 Comment

on 9 May 2013

I directly compared dW from all machines. They were the same. >> any(dW-dWother) ans = 0

Stability has been my best guess, but I have little knowledge of the dynamics of the system to verify whether or not its stable.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!