Message Boards Message Boards

Matrix operation speed: Mathematica vs Matlab?

Posted 8 years ago

Hello, I am new to Mathematica. I really enjoy the symbolic manipulation with numerics, however, simple matrix operation is really slow compare to Matlab. I think I am missing a point in Mathematica because the difference is huge.

ntimes = 2000;
pos = 10;
px = Range[0, 1, 1/pos];
f = x^2;


intp = ConstantArray[0, ntimes];
SetSharedVariable[intp];
AbsoluteTiming[
 ParallelDo[
   fint = f /. x -> px;
   intp[[i]] = fint.px;
   , {i, 1, ntimes, 1}];]
intT = Total[intp]

This takes 6.5 seconds where for Matlab (same computer of course)

    ntimes = 2000;
    pos = 10;
    px = 0:1/pos:1;
    intp = zeros(ntimes,1);
    tic
    parfor i=1:1:ntimes;
    f=px.^2;
    intp(i)=sum(f.*px);
    end
    toc
intT=sum(intp)

only takes 0.065 seconds. It is 100 times slower, that makes me believe that I am doing something incomplete in Mathematica.

Also I am aware that in this code f does not need to be in the loop but this is just an example. For my application f does or px does change in every do/for loop.

Thank you

Erdem

POSTED BY: Erdem Uguz
3 Replies

This is to illustrate a point made by @HenrikSchachner about speed gain from avoiding exact rational arithmetic.

Exact case:

ntimes = 200000;
posExact = 10;
px = Range[0, 1, 1/posExact];
AbsoluteTiming[ii = Table[(px^2).px, {i, 1, ntimes, 1}];]
intT = Total[ii]

(* Out[73]= {5.20341, Null}

Out[74]= 605000 *)

Approximate case:

ntimes = 200000;
posApprox = 10.;
px = Range[0, 1, 1/posApprox];
AbsoluteTiming[ii = Table[(px^2).px, {i, 1, ntimes, 1}];]
intT = Total[ii]

(* Out[88]= {0.0458801, Null}

Out[89]= 605000. *)

That's a factor of 100 or so.

POSTED BY: Daniel Lichtblau

Hi Erdem,

I guess in this case the parallelization is the problem. If you try the same without parallelization, it runs much faster:

ntimes = 2000;
pos = 10;
px = Range[0, 1, 1/pos];
f = x^2;
intp = ConstantArray[0, ntimes];
AbsoluteTiming[Do[fint = f /. x -> px;
   intp[[i]] = fint.px;, {i, 1, ntimes, 1}];]
intT = Total[intp]
(* Out:  {0.046800,  Null}  *)
(* Out:   6050  *)

Futhermore you should be aware of the fact that you are calculating using analytical (exact) expressions:

px
(* Out:  {0,1/10,1/5,3/10,2/5,1/2,3/5,7/10,4/5,9/10,1}  *)

If you are interested in a numerical value only then you should use those numerical values from the beginning, e.g. by defining:

px = Range[.0, 1.0, 1/pos];

This (seemingly tiny) change can/will speed up your code considerably.

Here is a nice link for you.

Regards -- Henrik

POSTED BY: Henrik Schachner
Posted 8 years ago

Thank you for your reply. You are right about the Do vs ParallelDo for a simple function + evaluation. And for the Approx approach you are right but sadly sometimes I will need more than floating points.

POSTED BY: Erdem Uguz
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract